-1
\$\begingroup\$

The script is attached to the object with the lights.

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

public class DetectInLight : MonoBehaviour
{
    public DimLights dimLights;
    public List<Light> lights = new List<Light>();

    private int layerMask = 1 << 8;

    private void Update()
    {
        IsOnDirectionalLight(lights[0]);
    }

    public bool IsOnDirectionalLight(Light light)
    {
        if (light.intensity == 0)
        {
            return false;
        }

        RaycastHit hit;
        Ray ray = new Ray(transform.position, -1 * light.transform.forward);

#if UNITY_EDITOR
        Debug.DrawRay(ray.origin, ray.direction, Color.red);
#endif

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
        {
            TurnLightsOn(true);


            Debug.Log("Hit !");

            return false;
        }
        else
        {
            TurnLightsOn(false);

            Debug.Log("No Hit !");
        }

        return true;
    }

    private void TurnLightsOn(bool on)
    {
        if (on)
        {
            StartCoroutine(dimLights.dimLightOverTime(2f, 0.1f));
        }
        else
        {
            StartCoroutine(dimLights.dimLightOverTime(0f, 0.1f));
        }
    }
}

The object that have the lights that turn of/off and the directional light are both on layer 8 name Detect Light

but it's never true.

when I put a breakpoint on the line

if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))

the value of layerMask is 256 and it's never turning on the lights.

I want the lights to turn on only when the shadow is from the directional light but I think that when the player is holding the object with the lights when I move the player the lights turning on. something maybe the player hand make shadow when the player is moving.

when the player is standing still the lights are off. this is why I want to exclude everything but the directional light.

maybe I'm wrong.

I can add a show video clip showing the problem.

This script is attached to empty gameobject and with this coroutine I turn the lights on/off :

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;
    }

    public void StopCourtines()
    {
        StopAllCoroutines();
    }
}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Don't you want to use the raycast to detect whether you've hit an object that blocks the light, NOT the light itself? The directional light's position doesn't matter, only its direction. You should not be counting on striking a specific point, just checking for a blocker. \$\endgroup\$
    – DMGregory
    Nov 19 at 23:12
  • \$\begingroup\$ @DMGregory that's the problem. the object with the lights that I turn on/off is in the player hand as child of the hand and something maybe the hand it self making shadow on it when the player is walking and when the player is moving and walking it's turning on/off the lights. if the player is standing still the lights off but when the player is walking the lights turn on/off like something like the hand making shadow on it. I want that when the player enter every shadow/darkness the lights will turn on the problem is with the player that make shadow too when walking. \$\endgroup\$ Nov 20 at 4:06
  • \$\begingroup\$ @DMGregory please look at this very short video clip I recorded showing the problem. I removed the layermask not using it and you can see in the video clip when the player is walking in the light in the desert the lights turn on off when he enter shadow the lights on and it's fine but when the player is outside of the shadow the lights tuning on/off when he walk. youtube.com/watch?v=lp8yptSvAJI \$\endgroup\$ Nov 20 at 4:13
  • \$\begingroup\$ @DMGregory when the player walk outside the shadow in the desert for example the lights should be off all the time but they are turning on/off and only when the player is walking. \$\endgroup\$ Nov 20 at 4:14
  • \$\begingroup\$ youtube.com/watch?v=lp8yptSvAJI \$\endgroup\$ Nov 20 at 4:14

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.