#Enemy Incorrectly Facing Player

1 messages · Page 1 of 1 (latest)

dark meadow
#

GODOT VERSION: v4.2.stable.official [46dc27791]

I'm trying to have an enemy face the player when the player enters its detection box. But I’m having trouble with it. The enemy doesn't always face the player (particularly when I'm trying to get it to face right after facing left).
Can anyone help me figure out what I’m doing wrong or link me to an answer for reference? I’ve looked around but couldn’t find anything.

obtuse pendant
#

I don't understand what directionX is doing here; is it being used anywhere else? And why are you checking that in the second if statement?

Are you just using it to test whether you've previously set the scale.x to 1, so you don't set the scale.x to -1 if it's already -1? That's mismatched with how you're handling the first case. You're resetting the scale.x to 1 every time there's a collision from the right, but you're only setting it to -1 if there's a collision from the left and there was previously a collision to the right. I don't know if that's a problem, but personally I'd try to have consistent behaviour.

Have you checked that the if-blocks are being run when you expect them to, such as by putting a print statement in each one?

dark meadow
# obtuse pendant ~~I don't understand what directionX is doing here; is it being used anywhere el...

Tried rewriting the code:

    collisionX = body.position.x
    print(collisionX - position.x)
    if body is Player:
        if collisionX > position.x:
            directionX = 1
            scale.x = 1
            print("this is facing right...it better be")
        if collisionX < position.x:
            directionX = -1
            scale.x = -1
            print("this is facing left...i hope so AAAAAAAAAAAAAAAA")
        $Label.text = (str(collisionX - position.x) + " | " + str(directionX) + " | " + str(scale.x))
        #$Label.scale.x = 1```

Both if statements run as expected (the first one when entered form the right, the second from the left), but oddly:
- The first one (right) doesn't change direction, even when supposed to
- The second one (left) does change direction, even when not supposed to
obtuse pendant
#

Hm... I can't see the video, but what's the pivot/origin of your enemy? And your player? Is it in the center of the sprite, or the left or right, or some custom offset?