Godot 4.2.2
I’m trying to implement the look_at() function because i need more control over it. The intention is that an entity will only move forward and it’s direction will be dependent on it’s rotations, kinda like tank controls but for the enemy.
the current code looks like this:
var angle_to_player = (Vector2(self.position.x, self.position.z) - Vector2(player.position.x, player.position.z)).normalized()
self.rotation.y = lerp_angle(self.rotation.y, -angle_to_player.angle(), 0.1)
var direction = transform.basis * Vector3.FORWARD
velocity.x = direction.x * run_speed
velocity.z = direction.z * run_speed
notice the use of lerp_angle just so it doesn’t sticks but rotates slowly.
The problem it’s that the entity will look from the side instead of forward (it’s left side will always look at the player). And so, will move around the player insted of straight into it. I suspect that i have to use the basis somewhere, but i don’t understand it well enough.
Another “problem” is that angle_to_player.angle() has to be negated:
self.rotation.y = lerp_angle(self.rotation.y, -angle_to_player.angle(), 0.1)
and i don’t really understand why even if it works.