Isn't the given solution somewhat over complicated?

I think the presented solution for the challenge seems to be way more complicated than would actually be needed.
If you wanted to set the rotation value immediately from when Start() calls the randomization method (which I think is fairly unnecessary given that it’s updated each frame anyway), one should be able to simply call the already existing RotateHead() method…
Also, given that all that RotateHead() depends on is the _currentRotation, we could simply go and set that value inside RandomStartingRotation()

OTOH there would be some easy adjustments to give the spotlights a bit more of a variation than just on which point of the PingPong path they start…

So I did it like this, which also skews the range of the rotation angles and the speed a little bit…

    [SerializeField] private float _rotationVariance = 5f;

    private void RandomStartingRotation()
    {
        _currentRotation = Random.Range(-_maxRotation, _maxRotation);
        _rotationSpeed += Random.Range(-_rotationVariance, _rotationVariance);
        _maxRotation -= Random.Range(0f, _rotationVariance);    // note: Don't go beyond the pre-defined max value
    }

I just gave that factor a moderate 5f for a subtle result…

Love the variances.

I also just set the _currentRotation to random (although, initially, I did it from 0 to _maxRotation)

Yeah, I also just set currentRotation to Random.Range(-maxAngle, maxAngle) and called it in Start, that’s all I needed to do.

Originally I created a SetZRotation method so that the intital starting point was set but since we cached the current rotation rather than getting and setting it directly from the transform, that didn’t work because as soon as update ran the currentRotation is 0, that’s when I realised all I had to do was set that currentRotation.

However, having looked at the code further and the functions I understand why it’s done the way it is and just setting currentRotation is not strictly correct, although it appears to work.

Part of the problem is the name currentRotation and part of it is that the solution is not explained at all.

Mathf.PingPong returns a number between 0 and the second parameter (our max angle), so the head isn’t moving between -45 and 45 degrees, its moving between 0 and 45, no matter what you set the starting rotation to be. The “currentRotation” value is actually a time value, not the rotation. Its an ever increasing number. I believe the reason he adds the max value to that random.range is so that currentRotation is always 0 or greater.

It doesn’t work exactly the way it seems either though. eg. Random.Range set one of my lights to -33, but the currentRotation then would be 12 and as soon as the update ran its z was set to 23.

The actual rotation of the head is never considered as part of the script, the rotation will always be between 0 and maxAngle

I believe the best solution is to just set currentRotation to Random.Range(0f, maxAngle);

That would get you closest to it starting at that angle

1 Like

Privacy & Terms