Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Simple light detection script

Discussion in 'Scripting' started by Ragnagna, May 13, 2024.

  1. Ragnagna

    Ragnagna

    Joined:
    Apr 28, 2024
    Posts:
    15
    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:
    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. public class LightSensor : MonoBehaviour
    5. {
    6.     [SerializeField] private float detectionRange = 10f; // Range within which the sensor can detect light
    7.     [SerializeField] private LayerMask detectionLayer = ~0; // Layer mask to filter which objects can activate the sensor
    8.     [SerializeField] private AudioClip activationSound;
    9.  
    10.     private AudioSource audioSource;
    11.     private bool activated = false;
    12.  
    13.     private void Start()
    14.     {
    15.         audioSource = GetComponent<AudioSource>();
    16.         if (audioSource == null)
    17.         {
    18.             audioSource = gameObject.AddComponent<AudioSource>();
    19.         }
    20.         audioSource.clip = activationSound;
    21.     }
    22.  
    23.     private void OnTriggerEnter(Collider other)
    24.     {
    25.         if (!activated && IsIlluminatedByLight(other.gameObject))
    26.         {
    27.             ActivateSensor();
    28.         }
    29.     }
    30.  
    31.     private bool IsIlluminatedByLight(GameObject otherObject)
    32.     {
    33.         Light[] lights = FindObjectsOfType<Light>();
    34.         foreach (Light light in lights)
    35.         {
    36.             if ((light.transform.position - transform.position).sqrMagnitude <= detectionRange * detectionRange)
    37.             {
    38.                 if (Vector3.Angle(light.transform.forward, transform.position - light.transform.position) < light.spotAngle / 2f)
    39.                 {
    40.                     if (detectionLayer == (detectionLayer | (1 << otherObject.layer)))
    41.                     {
    42.                         return true;
    43.                     }
    44.                 }
    45.             }
    46.         }
    47.         return false;
    48.     }
    49.  
    50.     private void ActivateSensor()
    51.     {
    52.         activated = true;
    53.         PlayActivationSound();
    54.         // Add any other event triggering code here
    55.     }
    56.  
    57.     private void PlayActivationSound()
    58.     {
    59.         if (activationSound != null && audioSource != null)
    60.         {
    61.             audioSource.PlayOneShot(activationSound);
    62.         }
    63.     }
    64. }
    and

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4.  
    5. public class LightSensor : MonoBehaviour
    6. {
    7.     [SerializeField] private Light lightSource; // Reference to the light source
    8.     [SerializeField] private AudioClip activationSound;
    9.  
    10.     private AudioSource audioSource;
    11.     private bool activated = false;
    12.  
    13.     private void Start()
    14.     {
    15.         audioSource = GetComponent<AudioSource>();
    16.         audioSource.clip = activationSound;
    17.     }
    18.  
    19.     private void OnTriggerEnter(Collider other)
    20.     {
    21.         Debug.Log("Trigger entered!");
    22.         if (lightSource != null && !activated)
    23.         {
    24.             if (IsIlluminatedByLight())
    25.             {
    26.                 activated = true;
    27.                 Debug.Log("Sensor activated!");
    28.                 PlayActivationSound();
    29.                 // Add any other event triggering code here
    30.             }
    31.         }
    32.     }
    33.  
    34.     private bool IsIlluminatedByLight()
    35.     {
    36.         if (lightSource == null)
    37.         {
    38.             Debug.LogWarning("Light source reference is null!");
    39.             return false;
    40.         }
    41.  
    42.         Vector3 toSensor = transform.position - lightSource.transform.position;
    43.         float angle = Vector3.Angle(toSensor, lightSource.transform.forward);
    44.         float lightAngle = lightSource.spotAngle;
    45.  
    46.         bool isIlluminating = angle < lightAngle / 2f;
    47.         if (isIlluminating)
    48.         {
    49.             Debug.Log("Sensor is being illuminated by the light source!");
    50.         }
    51.         else
    52.         {
    53.             Debug.Log("Sensor is not illuminated by the light source.");
    54.         }
    55.  
    56.         return isIlluminating;
    57.     }
    58.  
    59.     private void PlayActivationSound()
    60.     {
    61.         if (activationSound != null)
    62.         {
    63.             audioSource.PlayOneShot(activationSound);
    64.             Debug.Log("Activation sound played!");
    65.         }
    66.         else
    67.         {
    68.             Debug.LogWarning("Activation sound is not assigned!");
    69.         }
    70.     }
    71. }
     
  2. Ragnagna

    Ragnagna

    Joined:
    Apr 28, 2024
    Posts:
    15
    anybody?
     
  3. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    369
    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.
     
    ArachnidAnimal likes this.