- Home /
The question is answered, right answer was accepted
why is my character rotation always 180° after joystick is released.
object will rotate by the joystick angle but when its released rotation is 180. Part of the script :
var joystick : Joystick; //from mobile assets.
function Update () {
var An = Mathf.Atan2(-joystick.position.x,joystick.position.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(180 - An,Vector3.up);
}
Answer by DMGregory · May 06, 2014 at 09:14 PM
Because Mathf.Atan2(0, 0) returns zero. (The zero vector doesn't "point" anywhere, so zero was arbitrarily chosen so the function wouldn't blow up in this case)
You may want to add a deadzone, where if the joystick deflection is less than some threshold, you don't turn the character at all. Something like:
if(joystick.position.sqrMagnitude > 0.01) // Tune this float to taste.
{
var An = Mathf.Atan2(-joystick.position.x,joystick.position.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(180 - An,Vector3.up);
}
The code now won't be executed unless the joystick has some non-trivial deflection.
Follow this Question
Related Questions
Rotate a GameObject in the Vector3 direction? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Projectile Rotation To Match Direction Shot? Mobile Joystick 2 Answers
Keeping track of vehicle rotation? 1 Answer
Rigidbody rotation with virtual joystick, weird behaviour 0 Answers