Simple light detection script

Hello,

I am a noob at both unity and coding.

I am trying to do a simple thing… activate a sound when a specific light illuminate a specific objects.
I have tried various script from my good friend GPT and i cannot get it to work!

I have a point light within range of a sensor game object
Both are the same layer. Sensor has a collider, is trigger, and have the foolowing script attached to itwith the said point light attached to it as well as an audio file and source.

Can someone help me with this…?

Here are my 2 scripts:

using System;
using UnityEngine;
public class LightSensor : MonoBehaviour
{
    [SerializeField] private float detectionRange = 10f; // Range within which the sensor can detect light
    [SerializeField] private LayerMask detectionLayer = ~0; // Layer mask to filter which objects can activate the sensor
    [SerializeField] private AudioClip activationSound;

    private AudioSource audioSource;
    private bool activated = false;

    private void Start()
    {
        audioSource = GetComponent<AudioSource>();
        if (audioSource == null)
        {
            audioSource = gameObject.AddComponent<AudioSource>();
        }
        audioSource.clip = activationSound;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (!activated && IsIlluminatedByLight(other.gameObject))
        {
            ActivateSensor();
        }
    }

    private bool IsIlluminatedByLight(GameObject otherObject)
    {
        Light[] lights = FindObjectsOfType<Light>();
        foreach (Light light in lights)
        {
            if ((light.transform.position - transform.position).sqrMagnitude <= detectionRange * detectionRange)
            {
                if (Vector3.Angle(light.transform.forward, transform.position - light.transform.position) < light.spotAngle / 2f)
                {
                    if (detectionLayer == (detectionLayer | (1 << otherObject.layer)))
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private void ActivateSensor()
    {
        activated = true;
        PlayActivationSound();
        // Add any other event triggering code here
    }

    private void PlayActivationSound()
    {
        if (activationSound != null && audioSource != null)
        {
            audioSource.PlayOneShot(activationSound);
        }
    }
}

and

using System;
using UnityEngine;


public class LightSensor : MonoBehaviour
{
    [SerializeField] private Light lightSource; // Reference to the light source
    [SerializeField] private AudioClip activationSound;

    private AudioSource audioSource;
    private bool activated = false;

    private void Start()
    {
        audioSource = GetComponent<AudioSource>();
        audioSource.clip = activationSound;
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Trigger entered!");
        if (lightSource != null && !activated)
        {
            if (IsIlluminatedByLight())
            {
                activated = true;
                Debug.Log("Sensor activated!");
                PlayActivationSound();
                // Add any other event triggering code here
            }
        }
    }

    private bool IsIlluminatedByLight()
    {
        if (lightSource == null)
        {
            Debug.LogWarning("Light source reference is null!");
            return false;
        }

        Vector3 toSensor = transform.position - lightSource.transform.position;
        float angle = Vector3.Angle(toSensor, lightSource.transform.forward);
        float lightAngle = lightSource.spotAngle;

        bool isIlluminating = angle < lightAngle / 2f;
        if (isIlluminating)
        {
            Debug.Log("Sensor is being illuminated by the light source!");
        }
        else
        {
            Debug.Log("Sensor is not illuminated by the light source.");
        }

        return isIlluminating;
    }

    private void PlayActivationSound()
    {
        if (activationSound != null)
        {
            audioSource.PlayOneShot(activationSound);
            Debug.Log("Activation sound played!");
        }
        else
        {
            Debug.LogWarning("Activation sound is not assigned!");
        }
    }
}

anybody?

Well, what exactly don’t work? What part exactly? Have you tried debugging it? What do you expect from your code and what part fails? What is your scene setup?
Looking at the scripts, is nothing being called at all? Is OnTriggerEnter never called?

I don’t mean to be insulting, but 3 advices:

  1. Provide more context. “It doesn’t work” isn’t enough for other people to help you in most cases.
  2. Learn debugger, Visual Studio and Rider have one and it will solve 99% of your questions regarding things not working properly.
  3. ChatGPT and any AI is statistical model. Speaking from experience, it will give you 90% correct idea, but it won’t give you full solution most of the time, so you if you lack experience and knowledge, which is fine, you better start with something simpler, because non-working code you don’t understand usually don’t have any value for you.
2 Likes

Late to the party, but if you want to learn you can use the old version of my light detection plugin: GitHub - karoliso/Lumi: Unity Engine light detection script