How to check if RigidBody2D is touching object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By iota17

I am trying to check if my player (RigidBody2d) is colliding with any walls to prevent them from jumping. I was looking through some previous posts to see if I could get anything out of that but it didn’t seem to get me much.
Here is the script I have so far:

extends RigidBody2D

var up_delta = 0.0

func _physics_process(delta):
	if up_delta > 0:
		up_delta += delta
	contacts_reported = 1

func move(delta):
	if Input.is_action_pressed('right'):
		apply_central_impulse(Vector2(5, 0))
	if Input.is_action_pressed('left'):
		apply_central_impulse(Vector2(-5, 0))
	if Input.is_action_pressed('up'):
		if get_colliding_bodies() != null:
			if up_delta > 0.5:
				apply_central_impulse(Vector2(0, -1000))
				up_delta = 0.1
			elif up_delta == 0:
				apply_central_impulse(Vector2(0, -1000))
				up_delta = 0.1
	if Input.is_action_pressed('down'):
		apply_central_impulse(Vector2(0, 5))


:bust_in_silhouette: Reply From: magicalogic

You can do that in two ways:

  1. RigidBody2D has a get_colliding_bodies() function that returns an array of the bodies this RigidBody2D is colliding with.
  2. Connect the signal body_entered of the RigidBody2D to a function that will get called whenever another RigidBody2D collides with this one.