Enemy detects player but won't move right

Godot 4.2

So the problem I’m having is simple, I just can’t fix it for some reason. I made a player detection system for my enemy tree, it was working just fine until a few day’s ago. The enemy can move just fine with the “left_ground_detector”, but as soon as it goes to the “right_ground_detector” (no matter if the vector used is positive or negative ) the enemy just won’t move. I had checked to see if something was wrong with the collision but it was colliding just fine , the enemy just doesn’t move, I had remade the collision shape a few times to see if the child node itself was broken but that wasn’t the problem either, and the funny thing is, the enemy can move to the right, but only with the “Top_right_detector”. The conclusion I had came to was that somehow I had broken something in the editor itself, because no matter what version I reset to it still doesn’t work. If you got any ideas I’m all integer’s. B)

extends CharacterBody2D
@onready var left_air_detection = $“Detector’s/Top_left_detector”
@onready var right_air_detection = $“Detector’s/Top_right_detector”
@onready var left_detector = $“Detector’s/Left_detector”
@onready var right_detector = $“Detector’s/Right_detector”
@onready var HP = $HealthComponet.Health
@onready var Max_HP = $“HealthComponet”.Max_Health
@onready var Stun_clock = $“Stun_time”
@onready var Knock_timer = $“Stun_and_knockback_timer”
@onready var Ani = $“Entity look/AnimationPlayer”
@onready var Box = $“Hitbox/CollisionShape2D”

var detecting_player_right_ground = false
var DR: bool = false
var detecting_player_left_on_ground = false
var air_left = false
var air_right = false
var stun = 0
var hit = false
const SPEED = 150
const SPEED2 = 150
const JUMP_VELOCITY = -400.0
var times_hit_by_player: int = 0
var knockback_x =300
var knockback_y = 50

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(_delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity
if velocity.x == 0:
print(“not moving”)

if HP <= 0:
	queue_free()
ground_movement()
player_detection()

func player_detection():

if right_detector.is_colliding():
	detecting_player_right_ground = true
	DR = true
if  right_air_detection.is_colliding():
	air_right = true
	detecting_player_right_ground = false

if left_detector.is_colliding():
	detecting_player_left_on_ground = true
	
if  left_air_detection.is_colliding():
	air_left = true
	detecting_player_left_on_ground = false


if !right_detector.is_colliding():
	detecting_player_right_ground = false
	print("player is not to the right")

if !left_detector.is_colliding():
	detecting_player_left_on_ground = false

if !right_air_detection.is_colliding():
	air_right = false

if !left_air_detection.is_colliding():
	air_left = false

func ground_movement():
if DR == true or detecting_player_right_ground:
velocity.x = SPEED
print(velocity)
elif detecting_player_right_ground == false and !right_detector.is_colliding():
velocity.x = 0

if detecting_player_left_on_ground == true:
	velocity.x = -SPEED2
elif detecting_player_left_on_ground == false and !left_detector.is_colliding():
	velocity.x = 0
if air_left == true:
	velocity.x = -70
	print("in air on left")

if air_right == true:
	velocity.x = 70
	print("in air on right")

if !Stun_clock.is_stopped():
	velocity.x = 0
	print("stop")

move_and_slide()

When the player is detected to the right, you set the velocity to SPEED but then you check left too, and since the player isn’t detected on the left, you set the velocity to 0.

you could do something like:


if detecting_player_right_ground == true:
    velocity.x = SPEED
elif detecting_player_left_on_ground == true:
    velocity.x = -SPEED2
else:
    velocity.x = 0
1 Like

Thankyou, that makes sense I’mma try to fix this now. Truly , thank you.

1 Like

I had changed the if/elif statement’s that made the velocity change to zero. instead of checking if the collision shapes the bools where connected to where false, I have made so it checks the opposite collision shape.

if DR == true or detecting_player_right_ground:
velocity.x = SPEED
print(velocity)
elif detecting_player_right_ground == false and !left_detector.is_colliding():
velocity.x = 0

if detecting_player_left_on_ground == true:
	velocity.x = -SPEED2
elif detecting_player_left_on_ground == false and !right_detector.is_colliding():
	velocity.x = 0

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