Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

mpcarolin commented Aug 22, 2019

Worked like a charm. Thanks!

@demonixis

This comment has been minimized.

Copy link
Owner Author

demonixis commented Aug 23, 2019

Glad you liked it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.