Godot 4.0: 2D Ray Casting Help

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

I am having an issue where my 2D ray cast is not working after converting from 3.5.2. The ray target ($FloorChecker) value for y is set to 250 and not modified, this value and the following code is not working in 4.0, my old code was flipping an enemy using this script when reaching a cliff(no colliding) or walls. Everything but the raycast is working correctly:

@export var direction = -1
@export var detect_cliffs = true

func _ready():
	if direction == -1:
		$AnimatedSprite2D.flip_h = true
	
	$FloorChecker.target_position.x = $CollisionShape2D.shape.get_size().x * direction
	$FloorChecker.enabled = detect_cliffs
	
	$AnimatedSprite2D.play()
	
func _physics_process(_delta):
	calc_velocity = Vector2.ZERO
		
	if is_on_wall() or not $FloorChecker.is_colliding() and detect_cliffs and is_on_floor():
		direction *= -1
		$AnimatedSprite2D.flip_h = not $AnimatedSprite2D.flip_h
		$FloorChecker.target_position.x = $CollisionShape2D.shape.get_size().x * direction
:bust_in_silhouette: Reply From: keidav

Fixed my issues by setting:

safe_margin = 1

Now my enemy objects flip and move correctly when using a RayCast2D node.

I also found a tile was causing is_on_wall() to be true, making me think the raycast wasn’t working.