- Home /
Lerping euler angles make entire spin
I have this script to make a lerp to the Euler Angles of a Quaternion. When the second parameter of the Lerp (to) it's positive, works like a charm but when the second parameter it's negative, the Transform makes an entire spin.
if(yawNeg){
relativeRot = -(Vector3.Lerp(
rudders[0].localRotation.eulerAngles,//first parameter
(maxRot * ruddersVectorRot),//second parameter
Time.deltaTime*5));
} else if(yawPos){
relativeRot = Vector3.Lerp(
rudders[0].localRotation.eulerAngles,
maxRot * ruddersVectorRot,
Time.deltaTime*5);
} else {
relativeRot = Vector3.Lerp(
rudders[0].localRotation.eulerAngles,
Vector3.zero,
Time.deltaTime*5);
}
I've inverted the absolute value of the Lerp so I have my desired negative value. I know that the error resides on the yawNeg part.
What I'm doing wrong?
Here's a screenshot, I've taken it with Lightshot due to Unity Answers size limitation on images. http://prntscr.com/4f1y0t
EDIT: I've fotgot to say if I invert something, all messes up the rotation. Even if I invert only the second parameter or the whole Lerp. If the relativeRot Vector3 is negative, the rotation makes an entire spin.
Probably because you are negating the Lerped value, although I can see other potential issues with this approach as well. You meed to set your target rotation to the correct "negative yaw" position rather than negating the returned value. Try:
relativeRot = Vector3.Lerp(
rudders[0].localRotation.eulerAngles,//first parameter
-(maxRot * ruddersVectorRot),//second parameter
Time.deltaTime*5);
This will only work if "-(maxRot * ruddersVectorRot)" gives the correct target rotation for the max negative yaw position.
You should ideally use Quaternions directly to handle rotations. Just setup the max positive and max negative quaternion rotations then Lerp or Slerp using rudders[0].localRotation. You can get the reference Quaternions using Quaternion.Euler or Quaternion.AngleAxis, for example:
Quaternion maxPositiveRotation = Quatnerion.AngleAxis(maxRot, Vector3.up);
Quaternion maxNegativeRotation = Quatnerion.AngleAxis(-maxRot, Vector3.up);
Re you're edit: in that case "-(maxRot * ruddersVectorRot)" doesn't give the correct target rotation for negative yaw. Use quaternions ins$$anonymous$$d as I discuss above.
It's only really advisable to manipulate rotation vectors if you're modifying a single axis... in which case you might as well just Lerp the float representing the axis rotation rather than the Vector3. Anything dealing with multiple axis rotations should use Quaternions otherwise you'll be dealing with things like Gimbal lock, order or axis rotations (meaning you can't necessarily change one axis without affecting the others) etc.
@Huacanacha using Quaternion.Lerp with the maxPositiveRotation and maxNegativeRotation it only makes it glitch the rotation and it's even worse than using the Vector3.Lerp with euler angles. Also i've tried Lerping only the axis that need to be rotated but stills like the beggining, works like a charm on positive rotation but it's a disaster with a negative value
Add some debugs. What is "maxRot * ruddersVectorRot"? Is the standard rotation 0 degrees around the Y axis (i.e. you start at [0,0,0])?
How do you assign the relativeRot back to the models transform? Are you confusing localRotation with rotation anywhere? Add some more of the relevant parts of your script to your question so we can see exactly how this is used.
maxRot it's the max rotation ammount. Value:(float)45
ruddersVectorRot it's the axis for the rotation (So I can change it from the inspector) Value: (0,1,0) (It may change)
The standard rotation it's completely 0 on three axis
I assign the relativeRot to the eulerAngles of a new Quaternion and then I assign it to the Transform
tempRotation.eulerAngles = relativeRot;
rudders[0].localRotation = tempRotation;
Answer by joseques · Aug 22, 2014 at 01:35 AM
I've found the answer when I was coming back to home.
I've rotated the Transform so the rotation is 0,180,180. That way I can substract or add the rotations without using negative degrees.
Thanks @Huacanacha and @bustedkrutch for trying to help.
Answer by BakonGuy · Mar 05, 2015 at 05:59 PM
The accepted answer here is just a workaround, and will not work in all scenarios.
The real problem here is that there is not such a thing as a negative angle, only 0 degrees through 360 degrees. So when a negative angle is set, it is converting itself to a proper angle.
Because of this, you cannot cross zero
For example, you have an Angle of 10 and you are trying to Lerp to -10.
You start a lerp from 10 to -10
Said lerp gets to -1
Negative 1 is converted to 359 degrees
Lerp now tries to Lerp from 359 to -1, causing the spin
Mathf has a LerpAngle function which will treat the value as an angle in degrees, so it will for example Lerp from 350 to 10 by increasing value instead of decreasing. Using this function you can create a AngleLerp function to replace Vector3.Lerp like so
//Properly Lerp between two angles
Vector3 AngleLerp(Vector3 StartAngle, Vector3 FinishAngle, float t)
{
float xLerp = Mathf.LerpAngle(StartAngle.x, FinishAngle.x, t);
float yLerp = Mathf.LerpAngle(StartAngle.y, FinishAngle.y, t);
float zLerp = Mathf.LerpAngle(StartAngle.z, FinishAngle.z, t);
Vector3 Lerped = new Vector3(xLerp, yLerp, zLerp);
return Lerped;
}
Answer by bustedkrutch · Aug 21, 2014 at 07:10 PM
I'm not one of the experts, I'm just a hack, but I would try removing the ( - ) from -(Vector3.Lerp.. If the rotation is relative, perhaps when the second parameter is negative the direction is already taken care of.
If I'm wrong I'd hope you don't down vote me for trying.
Good luck.
I've tested it already, so many ways and still don't working. I know you're trying to help so I'll not downvote you.
THAN$$anonymous$$ YOU!!! I think next time I'll post in the safety of the "comments" section lol
Your answer
Follow this Question
Related Questions
How to add rotation to current rotation over time? 2 Answers
Two rotations at the same time, on the same axis. 1 Answer
How to smoothly rotate to certain directions using input axis 1 Answer
Rotation jumping to a different rotation after rotating a certain amount 0 Answers
lerp between two xyz rotation values 2 Answers