0
\$\begingroup\$

The problem is not the duration it take to turn off the light but when the whole turning off action start.

The first script is what make the lights turn off/on :

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

public class DimLights : UnityEngine.MonoBehaviour
{
    //Lights Change
    [Header("General Lights")]
    public Light[] lightsToDim = null;
    public bool lightsOnOff = false;
    public int lightsIntensity;
    public string lightsTag;
    public bool lightsDimmed = false;
    public SceneFader sceneFader;

    [Space(5)]
    [Header("Navi Lights")]
    public Light[] naviLights = null;

    private UnityEngine.GameObject[] myLights;

    private void Start()
    {
        if (lightsTag != "")
            myLights = UnityEngine.GameObject.FindGameObjectsWithTag(lightsTag);

        if (MenuController.LoadSceneForSavedGame != true)
        {
            for (int i = 0; i < lightsToDim.Length; i++)
            {
                if (lightsOnOff == false)
                {
                    lightsToDim[i].intensity = 0;
                }
                else
                {
                    lightsToDim[i].intensity = lightsIntensity;
                }
            }
        }
    }

    public void LightsState(bool state)
    {
        foreach (UnityEngine.GameObject go in myLights)
        {
            go.GetComponent<Light>().enabled = state;
        }
    }

    public IEnumerator dimLightOverTime(float toIntensity, float duration)
    {
        float counter = 0;
        //Get the current intensity of the Light 
        float[] startIntensity = new float[lightsToDim.Length];
        for (int i = 0; i < lightsToDim.Length; i++)
        {
            startIntensity[i] = lightsToDim[i].intensity;
        }

        while (counter < duration)
        {
            counter += Time.deltaTime;

            for (int i = 0; i < lightsToDim.Length; i++)
            {
                lightsToDim[i].intensity = Mathf.Lerp(startIntensity[i], toIntensity, counter / duration);
            }
            yield return null;
        }

        lightsDimmed = true;
    }
}

The second script is where I'm using it :

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

public class MoveToTarget : MonoBehaviour
{
    public enum TransitionState
    {
        None,
        MovingTowards,
        Transferring
    }

    public Transform destinationTransform;
    public bool isChild = false;
    public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    public float duration = 10.0f;
    public bool go = false;
    public DimLights dimLights;

    private float t;
    private Transform originTransform;
    private float timer;
    private TransitionState state = TransitionState.MovingTowards;
    private Vector3 originPosition;
    private SphereCollider col;
    private bool enableCollider = true;

    void Start()
    {
        t = 0.0f;

        curve.postWrapMode = WrapMode.Once;
        originPosition = transform.position;
        col = GetComponent<SphereCollider>();
    }

    void Update()
    {
        if (go)
        {
            if (col != null && enableCollider)
            {
                //col.enabled = false;
                //transform.GetComponent<Rigidbody>().isKinematic = true;

                Destroy(col);
                Destroy(transform.GetComponent<Rigidbody>());
                transform.GetComponent<InteractableItem>().enabledInteraction = false;

                enableCollider = false;
            }

            switch (state)
            {
                case TransitionState.MovingTowards:
                    var v = destinationTransform.position - transform.position;
                    if (v.magnitude < 0.001f)
                    {
                        state = TransitionState.Transferring;
                        originTransform = destinationTransform;
                        timer = 0;
                        return;
                    }

                    t += Time.deltaTime;
                    float s = t / duration;

                    transform.position = Vector3.Lerp(/*originPosition*/transform.position,
                        destinationTransform.position, curve.Evaluate(s));

                    break;

                case TransitionState.Transferring:
                    timer += Time.deltaTime;
                    this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);

                    if (timer >= 1.0f)
                    {
                        
                        this.transform.parent = destinationTransform;
                        transform.localPosition = new Vector3(0, 0, 0);
                        isChild = true;

                        StartCoroutine(dimLights.dimLightOverTime(0f, 1f));

                        state = TransitionState.None;
                        this.enabled = false;
                        return;
                    }
                    break;

                default:
                    this.enabled = false;
                    return;
            }
        }
    }
}

The problem in the second script is that I want to turn off the lights when the object become child at this part :

case TransitionState.Transferring:
                    timer += Time.deltaTime;
                    this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);

                    if (timer >= 1.0f)
                    {
                        
                        this.transform.parent = destinationTransform;
                        transform.localPosition = new Vector3(0, 0, 0);
                        isChild = true;

                        StartCoroutine(dimLights.dimLightOverTime(0f, 1f));

                        state = TransitionState.None;
                        this.enabled = false;
                        return;
                    }
                    break;

It's taking like 2-3 seconds until it's starting the DimLights Coroutine.

StartCoroutine(dimLights.dimLightOverTime(0f, 1f));

I'm not sure if the problem is in the DimLights script or in the second script.

The problem is that it's taking 2-3 seconds to start the lights turning off action.

What I want it to do once the object become child start turning off the lights in X seconds.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think that the problem with this code could be that it will start another coroutine every frame where the timer is larger than or equal 1.0f, not just the first time. Is it possible that you have a lot of coroutines running in the background which all compete for setting the light intensity? \$\endgroup\$
    – Philipp
    Nov 18 at 11:23
  • 1
    \$\begingroup\$ It also appears that you are building an own state machine which animates various properties. This looks to me as if you are reinventing the unity animation system. Perhaps this system would be a better approach to implementing this feature. (it's not just for characters. It can animate anything). \$\endgroup\$
    – Philipp
    Nov 18 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.