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!");
}
}
}