Quaternion.RotateTowards script working for one object but not for another.

Hi everyone,

Very new to all this and doing my best to find my own way through but this one has me stumped.

In a 2D orthographic game I have two search lights running the same script designed to rotate them, back and forwards, continuously between two angles around the z axis. There are three public variables, the turn rate and the max and min angles. These last two set the limits of the search arc. Certain angles seem to work, such as maxAngle = 320 and minAngle = 5 but most others do not.

I’ve put the code below and I’d really appreciate some fresh eyes and a nudge in the right direction.

public class SearchLight : MonoBehaviour {

    public int maxAngle;
    public int minAngle;
    public float turningRate;

    private Quaternion _targetRotation;
    private bool changeDirection;
    private Vector3 targetMaxAngle;


    // Use this for initialization
    void Start () {
        changeDirection = false;
        _targetRotation = Quaternion.identity;
        targetMaxAngle.z = maxAngle;
    }

    // Update is called once per frame
    void Update () {

        SetBlendedEulerAngles(targetMaxAngle);
        transform.localRotation = Quaternion.RotateTowards(transform.localRotation, _targetRotation, turningRate * Time.deltaTime);

        if (changeDirection == false)
        {
            targetMaxAngle.z = maxAngle;
            if (transform.localRotation.eulerAngles.z == targetMaxAngle.z)
            {
                changeDirection = true;
            }
        }

        if (changeDirection == true)
        {
            targetMaxAngle.z = minAngle;
            if (transform.localRotation.eulerAngles.z == targetMaxAngle.z)
            {
                changeDirection = false;
            }
        }

    }

    public void SetBlendedEulerAngles(Vector3 angles)
    {
        _targetRotation = Quaternion.Euler(angles);
    }

}

My aim is to be able to attach this script to any search light, sets its variables and let it get on with searching. My confusion is why does it work with one and not another?

Thank you very much indeed,

Pitcher

Solved thanks to your hint on comparing exact values.

It was indeed causing problems so compared them using Mathf.Approximately and all is working now. Not sure why certain angles worked with the exact comparison but at least it’s sorted now.

Thank you very much Bonfire Boy.