Error : "!CompareApproximately (aScalar, 0.0F)" with Quaternion.Lerp
I Everyone, i'm stuck with an error since a wile now and i hope one of you have a solution for me. Here is my code :
private void lookTarget(Vector3 pos){
Quaternion newRotation = Quaternion.LookRotation(transform.position - pos, Vector3.down);
newRotation.x = Quaternion.identity.x;
newRotation.z = Quaternion.identity.z;
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * 10);
}
And it give me sometimes this error :
!CompareApproximately (aScalar, 0.0F) UnityEngine.Quaternion:Lerp(Quaternion, Quaternion, Single)
I Try to found an answer on internet but without succes. If someone have the solution it will help me a lot.
Thank's everyone.
Answer by helgewl · Oct 26, 2016 at 02:28 PM
You probably have a (0, 0, 0, 0) Quaternion in either end of the lerp.
PS: I know this is an old question, but it was #2 in my Google search results.
What's this fix for this? (aka why can't you lerp to (0,0,0)?)
a Quaternion is undefined when it is (0,0,0,0).
I was getting this when my app was starting cause I didn't initialize it with Quaternion.identity or some real value. Once set, then the error was going away. If you don't want to initialize your Quaternion, you could do something like
private bool IsQuaternionInvalid(Quaternion q) {
bool check = q.x == 0f;
check &= q.y == 0;
check &= q.z == 0;
check &= q.w == 0;
return check;
}
Answer by kjuanlu · Mar 15, 2018 at 04:13 PM
The error happens when you use Time.deltatime as part of the third parameter. Try to change transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * 10);
by transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, 0.01f * 10);
Answer by Nishat11 · Sep 25, 2018 at 09:55 PM
Try this, as Quaternion.Lerp clamps between 0 & 1.
float temp_speed = Time.deltaTime * speed;
if(gameObject.transform.rotation != null && temp_speed < 1)
transform.rotation = Quaternion.Lerp(gameObject.transform.rotation, target_rotation, temp_speed);
for more understanding link text
Answer by qiaokeli_sucai · Aug 28, 2019 at 03:59 AM
The error happens when the third parameter is larger than 1 . Try to change transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * 10);
by
float t = Mathf.Clamp(Time.deltaTime * 10, 0f, 0.99f);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, t);