unity – How can I turn on/off lights automatic depending on the surrounding lights areas?

When the player enter this dark area I want the lights to turn on automatic and if the player is getting out this area to the desert area in the screenshot the light should be turn off automatic.

This screenshot is when the lights are turned off :

Lights turned off

And this is when the lights are turned on and this is how I want the lights to be when entering the dark area :

Lights turned on

The problem is how to detect when the dark area start and end how to decide what is dark enough to turn on/off the lights ?

For a start I try this script on the player hand child gameobject gameobject:

The player have in his hand a child object and this object have lights and this object shoot the raycast and should be turned on/off when in darkness or in ligt.

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

public class DetectInLight : MonoBehaviour
    public Transform directionalLight;

    void FixedUpdate()
        RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(transform.position,  directionalLight.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
            Debug.DrawRay(transform.position, directionalLight.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
            Debug.Log("Did Hit");
            Debug.DrawRay(transform.position, directionalLight.TransformDirection(Vector3.forward) * 1000, Color.white);
            Debug.Log("Did not Hit");

but the result is that the player is shooting the raycast in yellow down forward through the ground and not to the directional light direction :

The directional light is behind the player and the and the raycast shoot forward down the floor.

I’m not sure what to do.

enter image description here

Source link

More To Explore

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