Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- namespace BrokenWings.Props
- {
- public class MalfunctioningLight : MonoBehaviour
- {
- [Tooltip("If the light is on the same gameObject you can leave it alone.")]
- [SerializeField] private Light lightSource;
- [Tooltip("Array of light audio clips, you can leave it empty if you don't want any sound!")]
- [SerializeField] private AudioClip[] clips;
- [Space]
- [Header("Light Intensity:")]
- [SerializeField] private float offIntensity = 0.0f;
- [SerializeField] private float onIntensity = 2.0f;
- [SerializeField] private bool isInverted = false; // New parameter to invert behavior
- [SerializeField] private float sensitivity = 35.3f;
- [SerializeField] private float minRepeatTime = 1.0f;
- [SerializeField] private float maxRepeatTime = 5.0f;
- [Space]
- [Header("Material to turn On/Off the Emission Value")]
- [SerializeField] private Renderer emissionRenderer;
- [SerializeField] private Color emissionColor = Color.cyan;
- private AudioSource audioSource;
- private float[] samples = new float[256];
- private float repeatTimer;
- private Material emissionMaterial;
- private Color defaultEmissionColor;
- void Start()
- {
- SetupLightSource();
- SetupAudioSource();
- SetupEmissionMaterial();
- }
- void Update()
- {
- UpdateAudioPlayback();
- UpdateLightAndEmission();
- }
- private void SetupLightSource()
- {
- if (lightSource == null)
- {
- lightSource = GetComponent<Light>() ?? gameObject.AddComponent<Light>();
- lightSource.type = LightType.Point;
- lightSource.range = 5f;
- lightSource.shadows = LightShadows.Soft;
- }
- }
- private void SetupAudioSource()
- {
- if (clips.Length > 0)
- {
- audioSource = GetComponent<AudioSource>() ?? gameObject.AddComponent<AudioSource>();
- audioSource.spatialBlend = 1.0f;
- audioSource.spread = 1.0f;
- repeatTimer = Random.Range(minRepeatTime, maxRepeatTime);
- }
- }
- private void SetupEmissionMaterial()
- {
- if (emissionRenderer != null)
- {
- emissionMaterial = new Material(emissionRenderer.material);
- emissionRenderer.material = emissionMaterial;
- emissionMaterial.EnableKeyword("_EMISSION");
- defaultEmissionColor = emissionMaterial.GetColor("_EmissionColor");
- }
- }
- private void UpdateAudioPlayback()
- {
- if (audioSource != null && clips.Length > 0)
- {
- repeatTimer -= Time.deltaTime;
- if (repeatTimer <= 0)
- {
- PlayRandomClip();
- repeatTimer = Random.Range(minRepeatTime, maxRepeatTime);
- }
- }
- }
- private void UpdateLightAndEmission()
- {
- float targetIntensity = isInverted ? offIntensity : onIntensity;
- float currentIntensity = targetIntensity;
- if (audioSource != null && audioSource.isPlaying)
- {
- audioSource.GetOutputData(samples, 0);
- float rmsValue = CalculateRMS(samples);
- currentIntensity = isInverted
- ? Mathf.Lerp(offIntensity, onIntensity, rmsValue * sensitivity)
- : Mathf.Lerp(onIntensity, offIntensity, rmsValue * sensitivity);
- }
- lightSource.intensity = currentIntensity;
- if (emissionMaterial != null)
- {
- Color currentEmissionColor = emissionColor * (currentIntensity / onIntensity);
- emissionMaterial.SetColor("_EmissionColor", currentEmissionColor);
- DynamicGI.SetEmissive(emissionRenderer, currentEmissionColor);
- }
- }
- private float CalculateRMS(float[] samples)
- {
- float sum = 0f;
- for (int i = 0; i < samples.Length; i++)
- {
- sum += samples[i] * samples[i];
- }
- return Mathf.Sqrt(sum / samples.Length);
- }
- private void PlayRandomClip()
- {
- if (clips.Length > 0)
- {
- int index = Random.Range(0, clips.Length);
- audioSource.clip = clips[index];
- audioSource.Play();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment