Do something every few degrees of a rotation?

Hi,

I have an object which can be rotated. I want to execute something every 15 degrees of rotation.

Right now I set it up like this:

void Update()
{
    float currentAngle = this.transform.eulerAngles.z;

    if (currentAngle > 14.5f && currentAngle < 15.5f)
    {
        "do something"
    }

    .
    .
    .
}

and I have repeated this for every 15 degrees which works pretty well but seems way too complicated and not efficient at all to me.

Is there a more efficient way to approach this and if so how would you execute that?

Thanks in advance :slight_smile:

Disclaimer : Code not tested

private float step = 15;
private float deltaAngle = 1;
private bool stepReachedLastFrame;

void Update()
{
    float currentAngle = transform.eulerAngles.z;
    bool stepReached = (currentAngle + deltaAngle * 0.5f) % step < deltaAngle;
    if (stepReached && !stepReachedLastFrame)
    {
        Debug.Log("Entering zone");
    }
    else if (!stepReached && stepReachedLastFrame)
    {
        Debug.Log("Leaving zone");
    }
    stepReachedLastFrame = stepReached;
}