LookAt() pointing wrong direction

Godot Version

4.2.2.stable.mono

Question

Hello everyone, I am trying to get an enemy to look at an object as it passes the object.

When the scene starts I call LookAt(direction) in the enemy script. When the enemy gets near the object I call LookAt(Object) and it rotates the enemy to look at the object. When the enemy is no longer close to the Object I resume calling LookAt(Direction). However when I do this it points the enemy backwards, but continues moving in the correct direction.

I’m really confused at this behavior. If anybody could help explain what might be going on it would be greatly appreciated

Thanks

Edit: It looks like its changing my direction when using LookAt() I don’t understand that

Hard to say for sure without seeing some code, but a difference I notice in your description:

LookAt(Object) sounds like you’re calling LookAt on a position (in global coordinates?), while LookAt(Direction) sounds like you’re calling it on… well, a direction, which you presumably want to behave as an offset from where the player is (for example, if the player is at position (x, y, z), and the direction is (0, 0, 1), you would want to look at the global position (x, y, z+1)) - is that correct?

If so, that probably explains why it’s behaving differently.

Try using LookAt(direction, Vector3.UP, true), the third parameter is use_model_front this flips it around. Good if you are using Z forward models

Yes so its LookAt(GlobalPosition + direction)

Here is some code

private void OnDetectionAreaEntered(Node3D area)
    {
        if(area.IsInGroup("RotateBox"))
		{
			in_contact_with_rotate_box = true;
			look_at_position = area.GlobalPosition with {Y = GlobalPosition.Y};
		}
    }

    private void OnDetectionAreaExited(Node3D area)
    {
        if(area.IsInGroup("RotateBox"))
		{
			in_contact_with_rotate_box = false;
			look_at_position = chosen_dir;
		}
    }

in _PhysicsProcess

if(in_contact_with_rotate_box)
{
	LookAt(look_at_position);
}
else
{
    if (!GlobalTransform.Origin.IsEqualApprox(GlobalPosition + chosen_dir))  
    {
    LookAt(GlobalPosition + chosen_dir);
    }
}

I’ve Accounted for that already, but thanks. Speaking of when you import a model to Godot is it best to make sure that its facing in the -Z in the software you’re importing from?

Ok so I figured out my issue. The direction the enemy was headed was being rotated by the global rotation of the enemy before Velocity was calculated. However when I was calling LookAt(direction) I was not rotating that direction. So the enemy was moving in the correct direction, but not looking in the correct direction.

Thanks everyone for the replies!

1 Like

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