- Home /
How to go from eulerAngle axis 270 to 0 by rotating 90 degrees?
I have a script that rotates my character based on what key I am pressing. It will face forward, backward, left, right, and any where in between. I have it to where it will smoothly transition between rotations. All transitions are good besides the left to forward (270 to 0). It rotates the long way (270 degrees) instead of the short way (90 degrees). What could I do to make this transition 90 degrees but still go to 0? [Just the part of code to rotate]
Vector3 desiredRotationUp = transform.eulerAngles;
Vector3 desiredRotationDown = transform.eulerAngles;
Vector3 desiredRotationLeft = transform.eulerAngles;
Vector3 desiredRotationRight = transform.eulerAngles;
Vector3 eul = transform.eulerAngles;
if (input.y > 0 && desiredRotationLeft.y != 270)
{
desiredRotationUp.x = 0;
desiredRotationUp.z = 0;
desiredRotationUp.y = 0;
Debug.Log("Forward");
}
if (input.y < 0)
{
desiredRotationDown.x = 0;
desiredRotationDown.z = 0;
desiredRotationDown.y = 180;
Debug.Log("Backward");
}
if (input.x > 0)
{
desiredRotationRight.x = 0;
desiredRotationRight.z = 0;
desiredRotationRight.y = 270;
Debug.Log("Right");
}
if (input.x < 0)
{
desiredRotationLeft.x = 0;
desiredRotationLeft.z = 0;
desiredRotationLeft.y = 90;
Debug.Log("Left");
}
eul.y = (desiredRotationDown.y + desiredRotationUp.y + desiredRotationLeft.y + desiredRotationRight.y) / 4; ;
transform.eulerAngles = eul;
Answer by UnityCoach · Oct 27, 2018 at 08:34 PM
Hey there,
the best way to handle rotations is to use Quaternions, as EulerAngles will get you in many issues, including Gimbal Locking and crossing 360/0 degrees limit.
If you think of up, forward and right as directions (normalised vectors), you could sum up your "desired orientation" as two directions only, as those three vectors are and must remain ortho-normalised.
You can do something like this :
Vector3 _up = Vector3.up; // Vector3.up is shorthand for (0, 1, 0)
Vector3 _fwd = Vector3.forward;
if (Input ...)
{
// make this look right with up being up
_up = Vector3.up;
_fwd = Vector3.right;
}
else if (Input ...)
{
// make this look forward and down with up being up
_up = Vector3.up;
_fwd = (new Vector3 (0, 1, -1)).normalized;
}
// Then to assign this as a rotation, you could simply assign the forward and up of the transform :
transform.up = _up;
transform.forward = _fwd;
// Or prefer create a Quaternion and assign the rotation
transform.rotation = Quaternion.LookRotation(_fwd, _up);
// Then if you wanted to animate this over time, you could use Spherical Interpolation
Quaternion.Slerp(previousRotation, newRotation, delta);
// Or rotate towards the desired rotation
Quaternion.RotateTowards(currentRotation, desiredRotation, maxDelta);
Your answer
Follow this Question
Related Questions
Use a single rotation axis of a freely rotating object 1 Answer
Rotation Jumping values (0 to 180) 1 Answer
c# modify only one axis of a quaternion 2 Answers
Rotating a model with increments 1 Answer
Distribute terrain in zones 3 Answers