A
A
Alexey2017-04-28 20:16:58
C++ / C#
Alexey, 2017-04-28 20:16:58

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

1 answer(s)
D
Daniil Basmanov, 2017-04-28
@Alexeee

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 question

Ask a Question

731 491 924 answers to any question