Why does this rotation assignment cause fast oscillation (code and illustrated)

Hmm.

void Update()
{
    Quaternion groundRotation = Quaternion.identity;
    RaycastHit groundHit;
    if (Physics.Linecast(transform.position, transform.position + Vector3.down * 2, out groundHit))
    {
        groundRotation = Quaternion.FromToRotation(transform.up, groundHit.normal);
    }

    transform.rotation = groundRotation;
}

Now, I know exactly what the “problem” is - it’s that half the time groundRotation is Quaternion.Identity, so it’s setting the “correct” rotation with that in mind.

What I don’t get is how, during a frame where the cast gives a

erp

Okay I do get it (aint it funny how that works) one frame rotates it to the groundRotation. If you do the FromToRotation again then guess what, it’s Quaternion.Identity because because transform.up and groundHit.normal are the same vector, lol.

Okay so this works:

    void Update()
    {
        Quaternion groundRotation = Quaternion.identity;
        RaycastHit groundHit;
        if (Physics.Linecast(transform.position, transform.position + Vector3.down * 2, out groundHit))
        {
            groundRotation = Quaternion.FromToRotation(transform.up, groundHit.normal);
        }

        if (groundRotation != Quaternion.identity)
        {
        transform.rotation = groundRotation;
        }
    }

But also that’s maybe really stupid and I wonder what someone less dumb than me would do? Don’t worry about a smooth transition, I could always just throw a RotateTowards in there instead.

the struggle is real

    void Update()
    {
        Quaternion groundRotation = Quaternion.identity;
        RaycastHit groundHit;
        if (Physics.Linecast(transform.position, transform.position + Vector3.down * 2, out groundHit))
        {
            if (transform.up != groundHit.normal)
            {
                Debug.Log("A");
                groundRotation = Quaternion.FromToRotation(transform.up, groundHit.normal);
                transform.rotation = groundRotation;
            }
            else
            {
                Debug.Log("B");
            }
        }

        //transform.rotation = groundRotation;

    }

Maybe someone else sees it and it helps them realize what I have though so hey whatever.