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.
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.
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.
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
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.