Follow 2D Vector Direction

Hello everyone,

im currently working with the joystick in “Standards Assets”.
Im trying to get an object to always face the current joystick direction.
The joystick got 2 values : X and Y which both can get values between -1 and 1.

So for example:
If: X = - 1 and Y = 1
The Rotation of the object in euler should be: (0, 0, 45)

Anyone got an idea which formular or function i can use?

Greetings Keemo

If you take your XY values and put them in a Vector2, then normalise it, you can find the angle by doing ATan2 operation. I can’t remember exactly the order but it’s something like ATan2( MyVec.y , -MyVec.x ). I’m sure someone will probably correct me on that :slight_smile:

The result will be in radians so you need to multiply it by Mathf.Rad2Deg to get the euler Z rot

It worked!
Thank you very much :slight_smile:

The ATan2 operation:
ATan2( MyVec.y , MyVec.x )

If my math lecturer would find out that i didnt solved it myself, he would probably hit me :smile:

Or you could use LookRotation. If the joystick’s X is “inputX” and the joystick’s Y is “inputY”, and you want to rotate the transfrom “myTransform” to look in the direction the joystick is pointing, this will do it:

Vector3 inputDirection = new Vector3(inputX, 0, inputY);

myTransform.rotation = Quaternion.LookRotation(inputDirection);