The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

Look_at not turning enemy towards player

UnknownUserUnknownUser Posts: 0

I'm trying to get a simple enemy in my game to look at and track the player as it walks around.

Here a snippet of my code:

And here's what's happening:

It almost looks like the target for the look_at is not the character's position, but instead a multiple of the position - or something along those lines. I'm very new to Godot so unfortunately I'm a little lost right now.

Any help is greatly appreciated!

Best Answer

  • UnknownUserUnknownUser Posts: 0
    Accepted Answer

    Found the issue! I was going through the scenes per your instructions, and noticed something odd with the main character:

    Looks like the origin of the main character was offset from the actual position of the rendered sprite.

    I moved the sprite over the origin of the parent player character node:

    And the issue went away. Thanks for your help!

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 3,129Admin

    Welcome to the forums @Gradeun!

    The look_at functions rotates a node so that's its local Z axis faces the target position. My guess is that the flower body node's forward direction is not facing the local Z axis, which is why visually it doesn't appear to be tracking the target.

    When you select the flower body node with local space mode enabled in the editor (it's the little white cube above the viewport), does the forward direction of the flower body node face the Z axis?

    If it does not, then what I would recommend doing is either adjusting the 3D model so it faces in the correct axis, or parenting the visual aspects of the flower body so they face the local Z axis of the node you are calling look_at on. You could also adjust the rotation after calling look_at so the proper axis is facing forward through code. It depends on which axis the flower body node is facing though.

  • Thanks for your response!

    I went in and checked the local orientation. I think my flower is oriented so that it's facing the -Z direction:

    If I'm understanding how look_at works, shouldn't my model always be 180 degrees off, if look_at is orienting the +Z local axis of my model with the target? However:

    It seems like this isn't the case... I also tried rotating the flower body 180 degrees around in the script using rotate_object_local, but this didn't seem to have much of an effect

    Am I missing something?

  • TwistedTwiglegTwistedTwigleg Posts: 3,129Admin

    Does the Armature node face in the same direction, towards the negative Z axis?

    I do agree, it looks like it's off. I don't think you are missing anything, I think its just a configuration problem.

    One thing you can do to help debug the issue is attach a MeshInstance node to the Armature node (or whichever is having look_at called on it) and push it slightly in the direction of one of the three axes and run the game. If the new MeshInstance is pointing towards the target, then that's the axis the look_at function is using. I usually use a cube mesh for this and disabling depth drawing so it's always visible in the scene.

  • UnknownUserUnknownUser Posts: 0
    Accepted Answer

    Found the issue! I was going through the scenes per your instructions, and noticed something odd with the main character:

    Looks like the origin of the main character was offset from the actual position of the rendered sprite.

    I moved the sprite over the origin of the parent player character node:

    And the issue went away. Thanks for your help!

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file