Answer the question
In order to leave comments, you need to log in
How to rotate a 2D object?
Hello. I am writing with a trivial question. I saw a bunch of examples on this issue, but they do not want to work in any way.
What you need: rotate the 2D object in the direction of the joystick.
Tried LookAt(), Rotate(), and more. Most of the examples were for the mouse pointer, please help with the joystick.
public float angleBetween;
transform.rotation = Quemertion.AngleAxes(angleBetween, Vector3.forward);
In this case, the object is rotated by angleBetween, which is specified in the inspector.
But I can't calculate the angle of the joystick.
Answer the question
In order to leave comments, you need to log in
First, through Input.GetAxis , you get the horizontal and vertical position of the joystick, you get a vector in the XY plane. Then using Quaternion.FromToRotation you get the rotation around the Z axis.
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
var vector = new Vector3(horizontal, vertical , 0);
transform.rotation = Quaternion.FromToRotation(Vector3.up, vector);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question