How do I make the RayCast2D rotate properly?

Godot Version

4.6.2 stable

Question

I’ve been trying to rotate a RayCast2D node into always looking at the player, but for some reason it either doesn’t rotate or doesn’t work when you go on the right of the parent of the raycast (an enemy node for now). Does anyone know how to fix this?
This is the code for the Raycast node.


@onready var EnemyRaycast : RayCast2D = $EnemyRaycast

@onready var Player_body : Player = $"../player"


func _physics_process(delta: float) -> void:
	
	if not is_on_floor():
		velocity += get_gravity() * delta
	
	EnemyRaycast.look_at(Player_body.global_position)

	EnemyRaycast.force_raycast_update()

	if EnemyRaycast.is_colliding():
		var collidedWith = EnemyRaycast.get_collider()
		if collidedWith == Player_body:
			print("collided_with_player")
	
	
	
	move_and_slide()

look_at() points the positive X direction to the target. Set the local ray target property to be down the X axis instead of default Y axis.

Sooo just for clarification, do I set the target position to something like:

EnemyRaycast.target_position = Vector2(50, 0)

Is that what you mean?

Yes. look_at() will then rotate node’s whole coordinate system so its x axis points at the actual target.

Sorry, it still isn’t working. I’ve changed the code to this:

EnemyRaycast.target_position = Vector2(50, 0)

EnemyRaycast.look_at(Player_body.global_position)

EnemyRaycast.force_raycast_update()

if EnemyRaycast.is_colliding():
	var collidedWith = EnemyRaycast.get_collider()
	if collidedWith == Player_body:
		print("collided_with_player")

Thanks for the previous help though

Enable “Visible Collision Shapes” in the Debug menu to visually check where the raycast is actually pointing

Thanks, this helped. I realized my Raycast was pointing at the wrong angle, so boom: all I had to do was rotate it 50 degrees in the right direction. This might be different for other people making other projects though; just tinker until you get it.