How to go back and forth two values starting from a random value inbetween

Hello everybody.

The question is (fairly) simple, solution maybe not.
I’d want my material color redValue to ping pong from minValue to maxValue starting from a random redStartValue.

So I thought to use Mathf.Lerp to interpolate value, passing PingPongValue as t parameter.

Thing is that as redValue ever start from 0 and not from redStartValue.

any ideas how to solve this?

thank you!

public class LerpedColours : MonoBehaviour
{
    public float redValue;
    public float redStartValue;
    public MeshRenderer render;
    public float minValue = 0f;
    public float maxValue = 1f;
    static float pingPongLerp;
    // Start is called before the first frame update
    void Start()
    {
        redStartValue = Random.Range(0f, 1f);
        Material material = render.material;
        material.color = new Color(redStartValue, 0, 0);
    }
    // Update is called once per frame
    void Update()
    {
        Material material = render.material;
        pingPongLerp = Mathf.PingPong(Time.time, 1f);
        redValue = Mathf.Lerp(minValue, maxValue, pingPongLerp);
        material.color = new Color(redValue, 0, 0);
    }
}

Let me only suggest to you to review your variable names, names should say what this variable is created for. Also review your accessor modifiers, you basically set all to public and I don’t know if this is really what you want

public class LerpedColours : MonoBehaviour
{
    public float redValue;
    private float redStartValue; // If you set this in start, does not need to be exposed in inspector
    public MeshRenderer render;
    public float minValue = 0f;
    public float maxValue = 1f;

    // this name isn't very clearly, change to something like currentPercentage
    // why static?
    static float pingPongLerp;

    private Material material;
   
    // Manually increasing my time from an starting point
    private float myTimeCounter = 0f;
    // Start is called before the first frame update
    void Start()
    {
        // Initial value for timer
        myTimeCounter = Random.Range(0f, 1f);
       
        // redStartValue = Random.Range(0f, 1f); // We don't need this anymore
        // Material material = render.material; // If you will not cached this info, don't need to make this step
       
        // Cache material
        material = render.material;
        // material.color = new Color(myTimeCounter, 0, 0); // Update also runs on frame 1, so this is being duplicated in first frame, so this part is somekind of optional
    }
    // Update is called once per frame
    void Update()
    {
        // Material material = render.material; // Don't
       
        // As Time.time will always start in 0, you will always start in minValue
        // pingPongLerp = Mathf.PingPong(Time.time, 1f);
       
        // Control your percentage manually increasing your timer
        pingPongLerp = Mathf.PingPong(myTimeCounter, 1f);
           
        redValue = Mathf.Lerp(minValue, maxValue, pingPongLerp);
        material.color = new Color(redValue, 0, 0);

        myTimeCounter += Time.deltaTime; // time spent in the last frame
    }
}
1 Like

Thank you Nefisto. Variables are set to public so I could read the values in the inspector and understand what was happening. Once that the script works I would have set the variables to private!
Also thanks for suggestions about variable names. I supposed that time.time could be an issue.

1 Like