0 votes


I've got a player which moves by mouse click. What I need to do is to determine direction in such a way that I could apply each of 8 animations.

I found tutorials concerning arrows movement, but couldn't find anything about mouse click movement.

enter image description here

Every direction in this picture implies specific animation. How can I determine these directions?

in Engine by (15 points)

1 Answer

+1 vote
Best answer

Hello DeadZombie,

You might want to use the return value of "moveandslide". From the documentation : "Returns the movement that remained when the body stopped.". As such, this method returns a Vector2 that is exactly that, the movement that remained. If you want the angle, you will have to use the "angle_to" method.

I haven't tested what I'm writing, but to get this direction (which needs the angle), I'd do that :

var resultingMovement : Vector2 = move_and_slide(...)
var movAngle : float = resultingMovement.angle_to(Vector2(0,1))

The variable "movAngle" will be in radians and work as such : its value will be around 0 is the resulting movement goes upward, close to PI/2 if the body is going left, -PI/2 if it is going right etc. From that you can just put 8 conditions (for 8 directions) and check if the angle is within a certain interval.

by (315 points)
selected by
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 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.