Unity Increasing a value and then decrease it (DayNightCycle)

Viewed 2042

I have a cycle and want the cycle bar, having a value range from 0 to 1 and back to 0.

enter image description here

So, currently I use this code

public class DayNightCycle : MonoBehaviour
{
    private float currentTime = 0; // current time of the day
    private float secondsPerDay = 120; // maximum time per day
    private Image cycleBar; // ui bar

    private void Start()
    {
        cycleBar = GetComponent<Image>(); // reference
        UpdateCycleBar(); // update the ui
    }

    private void Update()
    {
        currentTime += Time.deltaTime; // increase the time
        if (currentTime >= secondsPerDay) // day is over?
            currentTime = 0; // reset time

        UpdateCycleBar(); // update ui
    }

    private void UpdateCycleBar()
    {
        cycleBar.rectTransform.localScale = new Vector3(currentTime / secondsPerDay, 1, 1);
    }
}

but now I want a behaviour as mentioned by the picture above. How can I increase currentTime from 0 to 1 and then back to 0?

The problem: My cycle bar should still increase from the left to the right.

The night should last 40% of the maximum time, the other ones 20%.

3 Answers
Related