unity – Why the layer mask when using raycast is not working as supposed to?


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



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email