+1 vote

I'm trying to create a top-down space shooter like Asteroids, where all of the ships are RigidBodies. I want to make it so that the enemies always turn to face the player, which is done through the below code:

func _integrate_forces(state):
    [...]
    var player_vector = (player.position - position).normalized()
    set_applied_torque((rad2deg(player_vector.angle()) - rotation_degrees + 90) * spin_speed)

For the most part, this works fine. Unless the player is to the lower-left of the enemy, in which case the enemy begins to spin uncontrollably,

Spinning

First of all, is this the best way to implement this sort of behavior? If so, what am I doing wrong? If not, what is the best way to do this?

in Engine by (38 points)

I've found that a simple way to do it is to replace the last line of code with rotation = player_vector.angle() + PI / 2, but I'd prefer a solution that actually applies torque rather than instantly changing the rotation, as I could see such rapid movement causing problems.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.