- Home /
How i stay the rotate with joystick?
Hello I have a problem since when I move my UI joystick the character moves and rotates to the direction in which it moves but when you release the joystick the rotation returns to 0 I need the rotation to stay
sorry for my english i used Google Translate
void Update() {
float angle = Mathf.Atan2(joystick.Vertical(), joystick.Horizontal()) * Mathf.Rad2Deg;
transform.GetChild(0).rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
Answer by Eno-Khaon · Sep 21, 2017 at 12:07 AM
A quick change should make this work well enough to start:
Vector2 joy = new Vector2(joystick.Horizontal(), joystick.Vertical());
if(joy.sqrMagnitude > 0.001f)
{
float angle = Mathf.Atan2(joy.y, joy.x) * Mathf.Rad2Deg;
transform.GetChild(0).rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
Since it's only necessary to change your facing while moving, you can check whether or not input is actively provided and work from there.
I finally got it looking for this like 2 weeks ago it was all a headache thank you very much :D
Your answer
Follow this Question
Related Questions
Directly Rotate a Hinge? 1 Answer
Rotate An Object Toward The Direction The Joystick Is Pressed, can you help me? I am stuck 1 Answer
Rotating a ship with joystick and keeping camera centered. 0 Answers
FPS Camera rotates on Z when using a joystick 0 Answers
Joystick 2D Sprite Rotation and Movement 0 Answers