SmoothDampAngle problem when the target angle is 90 - 270 degree

Hi guys,
I want to use Mathf.SmoothDampAngle to rotate an object which have to follow the given angle. I works well when the given degree is from 0 to 90 and from 180 to 360, but when it turns from 91 to 179 the target object’s rotation is totally messes out.

this is my code:

void Update () {
        var damp = Mathf.SmoothDampAngle(transform.eulerAngles.x, /*Normalise(*/gc.getTravelledDeg()/*, 90, -90)*/ + 10.0f, ref smoothVelocity, 0.5f);
        transform.rotation = Quaternion.Euler(damp, 0, 0);
	}

    //Normalizes any number to an arbitrary range 
    //by assuming the range wraps around when going below min or above max 
    float Normalise( float value, float start, float end ) 
    {
        float width = end - start;   // 
        float offsetValue = value - start;   // value relative to 0

        return ( offsetValue - ( Mathf.Floor(offsetValue / width ) * width ) ) + start ;
        // + start to reset back to start of original range
    }

I have tried to normalise the angle. I only got a partial result when I used the normalisation from 90 to -90, but it is not what I want. I really don’t know what is the problem with my code. Please, help me…

Your problem is that you do a read-modify-write operation on eulerangles which doesn’t work as the rotation is internally stored as Quaterion. When you read the current eulerAngles Unity has to convert the Quaterion to euler angles. This conversion is not unique as the same rotation can be represented with different euler angles combinations.

You may want to use your own float variable to store the current rotation without reading it back from eulerAngles.

As you might know euler angles suffer from gimbal lock at x==90° or 270° while quaterions do not have this problem. Gimbal lock generally makes any kind of interpolation between two rotations difficult. That’s why we actually use Quaternions.

Figured it out:

void FixedUpdate () {
        Quaternion desiredRotation = Quaternion.Euler(gc.getTravelledDeg() + 45, 0, 0);
        Quaternion smoothedRotation = Quaternion.Slerp(transform.rotation, desiredRotation, smoothVelocity);

        transform.rotation = smoothedRotation;
    }

It works! :slight_smile: