Skip to content

Instantly share code, notes, and snippets.

@demonixis
Last active January 27, 2022 19:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save demonixis/6475766 to your computer and use it in GitHub Desktop.
Save demonixis/6475766 to your computer and use it in GitHub Desktop.
a simple script for varying the intensity of light with Unity 3D
using UnityEngine;
using System.Collections;
public class BlinkLight : MonoBehaviour
{
Light light;
public float minIntensity = 0.0f;
public float maxIntensity = 1.5f;
public float frequency = 0.01f;
public float phase = 0.0f;
public AudioClip maxSound = null;
void Start ()
{
light = GetComponent<Light>();
}
void Update ()
{
float x = (Time.time + phase) * frequency;
x = x - Mathf.Floor(x); // normalized value to 0..1
light.intensity = maxIntensity * Mathf.Sin(2 * Mathf.PI * x) + minIntensity;
if (light.intensity >= (maxIntensity - 1) && maxSound != null)
audio.PlayOneShot(maxSound);
}
}
@mpcarolin
Copy link

Worked like a charm. Thanks!

@demonixis
Copy link
Author

Glad you liked it!

@sharkspread
Copy link

signed up specially to thank you for this excellent script

@demonixis
Copy link
Author

Thanks!

@Claudiu0707
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment