NaN Protection

Is there any reason not to do it the way I have shown below?

using UnityEngine;

[DisallowMultipleComponent]
public class Oscillator : MonoBehaviour
{
    [SerializeField] Vector3 movementVector;
    [SerializeField] [Range(0.001f, 2f)] float rate;

    Vector3 startingPostion;
    float step;
    float position;
    // Start is called before the first frame update
    void Start()
    {
        startingPostion = transform.position;
        step = 0f;
    }

    // Update is called once per frame
    void Update()
    {
        step = step + 0.1f * rate;
        position = Mathf.Sin(step);
        Vector3 offset = movementVector * position;
        transform.position = startingPostion + offset;
    }
}

The rate is more generic and can't possibly end up with the issue of having a NaN warning even without protection.

Hi Nathaniel,

That’s a good alternative solution. Well done. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms