- Home /
How to rotate 2D object to location of joystick?
I have a top down 2D scene where the player moves up/down in the z axis and left/right in the x axis. I'd like to rotate the object to the point in the same direction as the the joystick and reflected instantaneously, not linearly too if possible.
I'm not sure what the proper math is to do this but I have the inputs below...
float horizontal = Input.GetAxis("RightAnalog_horizontal") * Time.deltaTime * moveSpeed;
float vertical = Input.GetAxis("RightAnalog_vertical") * Time.deltaTime * moveSpeed;
I've tried a few ways of doing this including the following rotations with no luck...
shootPoint.Rotate(new Vector3(0, hr, vr));
shootPoint.rotation = Quaternion.Euler(0.0f, (Mathf.Atan2(vertical , horizontal ) * Mathf.Rad2Deg), 0.0f);
Any help would be greatly appreciated.
Answer by adrianmaurer · Apr 05, 2013 at 01:19 PM
So this morning I had a realization and discovered that my vertical input was not registering because of my input settings. And then I rotated the adj/opp tangent sides and everything was working perfectly.
float angle = Mathf.Atan2(horizontal, vertical) * Mathf.Rad2Deg;
shootPoint.rotation = Quaternion.Euler(new Vector3(0, angle, 0));
I'm still new to unity but I'd like to see if there is any alternative answers to this question.