Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can Random.Rotation cause CompareApproximately error?

Discussion in 'Scripting' started by bigpants, Aug 25, 2019.

  1. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    48
    Occasionally, my Quaternion.Lerp will cause the CompareApproximately error in Unity.
    i.e. newRotation = Quaternion.Lerp(rotationStart, rotationEnd, 0.5f * Time.deltaTime);

    rotationStart and rotationEnd were created using Random.Rotation
    I'm 99% sure that NO other manipulation was done to them
    i.e. Those rotations are NOT ZERO Quaternions (0, 0, 0, 0),
    nor the result of division by zero errors converted to Quaternion

    Time is guaranteed to be > 0 and <= 1f

    More info on CompareApproximately here:
    https://forum.unity.com/threads/quaternion-lerp-problem-compareapproximately-ascalar-0-0f.154218/


    MY THOUGHT
    Can Random.Rotation produce the dreaded ZERO Quaternion (0,0,0,0) or other "bad" rotations? If so:
    a) that explains the problem I occasionally see
    b) everyone should immediately stop using Random.Rotation
    c) everyone should switch to this: Quaternion.Euler(Random.onUnitSphere * 180f)


    HOW TO PRODUCE CompareApproximately ERROR
    Creating a ZERO Quaternion (0, 0, 0, 0) is not possible. Need to create indirectly:
    Vector3 v = Vector3.one;
    v /= 0f;
    Quaternion r = Quaternion.Euler(v);
    https://answers.unity.com/questions/672325/compareapproximately-det-10f-005f.html
     
  2. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    48
    I figured out my problem!
    rotationEnd WAS messed up due to a prior function (took forever to find).
    My code, and NOT Random.Rotation, was causing my CompareApproximately ERROR.

    Conclusion:
    Random.Rotation is SAFE to use and does NOT generate the dreaded ZERO Quaternion