I need help with direction switches in movement

I’m trying to code an enemy that switches it path from left to right and right to left when facing a wall in 2D

func _process(delta: float) → void:
if Ray_cast_Right.is_colliding():
Direction = -1
if Ray_cast_Left.is_colliding():
Direction = 1

position.x += Direction * SPEED * delta

Whats happening when you run this code?

it gives me 2 errors
Identifier “Ray_cast_Right” not declared in the current scope.
Identifier “Ray_cast_Left” not declared in the current scope.

That’s because you did not declare the raycasts as variables.

1 Like

You can click and drag the ray cast node on to your code and hold ctrl before releasing, then you can do the following.

@onready var ray_cast_left = Ray_cast_left #node name

func _process(delta):
if ray_cast_left.is_colliding():
direction = 1

Do the same for the right ray cast

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.