Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

unity3d - Unity Mathf.PingPong temperature between Max to Min over day time

I'm trying to simulate the sun temperature based on the day time. Assuming that at 12:00 it should be tempMax and at 24:00 tempMin how can I PingPong between the 2 values cyclically?

    private void Update()
{
    hoursText.text = "time: "+ hours.ToString("00");
    daysText.text = "day: " + days.ToString();
    tempText.text = temperature.ToString("00") + " °C";


    if (hours < 24)
    {
        hours += Time.deltaTime ;
        rotations.z = hours * 360 / 24;
    }
    if (hours >= 24)
    {
        hours = 0;
        days += 1;
    }

    temperature = Mathf.PingPong(1/hours, tempMax) - tempMin;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Mathf.PingPong to get a t value then Mathf.Lerp to convert that to a temperature:

temperature = Mathf.Lerp(tempMin, tempMax, Mathf.PingPong(hours/12f, 1f));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...