Enemy dosent go forward and backwards as intended

Godot Version

4.5.6

Question

i tried making script that whould make enemy turn arroung when he comes close to a wall edge using raycats also tried adding something that will prevent player from activating it

extends Node2D


@onready var fr: RayCast2D = $FR
@onready var wl: RayCast2D = $WL
@onready var wr: RayCast2D = $WR
@onready var fl: RayCast2D = $FL

var direction = -1

func _process(delta):

	position.x += 120 * delta * direction
	
	
	if  not fr.is_colliding():
		if not (fr.get_collider() is Player):
			direction = -1

	if  not fl.is_colliding():
		if not (fl.get_collider() is Player):
			direction = 1

	if wr.is_colliding():
		if not (wr.get_collider() is Player):
			direction = -1

	if wl.is_colliding() :
		if not (wl.get_collider() is Player):
			direction = 1

but the effects of this code are that so it ignores walls ( same effects if i remove if its not a player check )
it sometimes detectes something but it feels really random

Your second ledge is slightly lower than the other, the raycasts cannot detect the floor if they are wholly inside it

1 Like

Try moving the raycasts that are pointing down a little higher than the enemy’s lowest point is. Currently it’s not detecting the first floor, because the raycast is inside the floor.

1 Like

You have a not in the first 2 if is_colliding()

yeah thoes are for checking if the floor ends should have said in original post :sweat_smile:
moving them a bit up fixed the issue

1 Like

Lol ok. Is suggest moveing your code to the physics process since it’s collision based.

As a side note of highly recommend getting in the habit of naming your variables better. You know what fl, fr etc are right now, but if you come back to this code in 6 months you’ll have no idea. I’m talking from experience here too :smiley:

1 Like