Search code examples
c#unity-game-enginelightinglight

How to I make this script stay in day and night longer?


I don't quite understand this code so I am having difficulty modifying it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SunScript : MonoBehaviour {
    public float duration = 1.0F;
    public Light lt;
    void Start() {
        lt = GetComponent<Light>();
    }
    void Update() {
        float phi = Time.time / duration * 0.1f * Mathf.PI;
        float amplitude = Mathf.Cos(phi) * 0.5F + 0.5F;
        lt.intensity = amplitude;
    }
}

This moves the light's intensity up and down in a cycle. However, I would like to have it stay at its brightest and its dimmest for a while before it starts to move back the other direction. How should I do this?


Solution

  • What about replacing Mathf.Cos(phi) one of these functions instead of just a cosine?

    Using the equation at the bottom of that page:

    float amplitude = Mathf.Sin(Mathf.PI / 2f * Mathf.Cos(phi)) * 0.5f + 0.5f;
    

    For the formula with the b term, you could do this (using extra temp variables to make it a little more readable).

    float b = // whatever your b parameter is or have this declared as a class field that you can set in the Unity editor
    float cosine = Mathf.Cos(phi);
    float numerator = 1f + (b * b);
    float denominator = 1f + (b * b * cosine * cosine);
    float amplitude = (Mathf.Sqrt(numerator / denominator) * cosine) * 0.5f + 0.5f;