- Home /
How do I animate a pulsing light?
Hi, so I haven't really worked with animating lights before, and am trying now with Unity. I have my entire program working, but as an added effect, I want to make it so that, on button press, a point light variable attached to my Particle System will have its range quickly animate from 0 to a chosen number (Let's just say 5) and go back to 0, creating a sort of pulsing or flashing light effect. However, I want it to animate it so that there's a smooth transition between the start and end points of the animation within a short span of time. While I know how to access the range functions, I'm not exactly sure how to code this.
Can anyone help me with this? (Preferably in C#)
want the light in that centre to animate out and in on button press*
If you a linear transition is okay, you can use $$anonymous$$athf.PingPoint() to create a flashing effect. If you need a more eased transition, you can use $$anonymous$$athf.Sin().
I don't know how to code that though. I'm not a good programmer. What should I do here?
using UnityEngine;
using System.Collections;
public class LightFlashC : $$anonymous$$onoBehaviour {
public float lerpTime = 0.5f;
private float i = 0;
// Use this for initialization
void Start () {
light.range = 0;
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)) {
light.range = $$anonymous$$athf.Sin(5,10);
i+= Time.deltaTime;
}
}
}
Answer by robertbu · Apr 03, 2013 at 12:21 AM
Based on your use of light.range above, here is a script that when attached to a point light scales the range:
#pragma strict
var maxDist = 30;
var speed = 40.0;
function Update () {
light.range = Mathf.PingPong(Time.time * speed, maxDist);
}
That doesn't exactly work the way I want it to. It just suddenly becomes a random range under 30 and it doesn't animate itself back to 0.
Also, I don't understand the meaning behind time.time * speed.
Do this setup:
Start with an empty scene.
Put a cube at (0,0,0) (with the default material).
Create a point light at and place it at (0,5,0)
Attach this script above to the light
Hit play
What it does is animate the range of the light. It cycles from 0 up to maxDist and then back down to 0 repeatedly. It is easy to see with this setup. You may want to animate the intensity ins$$anonymous$$d. If so, I'd start with a maxDist setting of 8 and a speed setting of 10.
Time.time drives the PingPong. 'speed' deter$$anonymous$$es how fast. 'speed' is not an absolute value, but ins$$anonymous$$d is relative to maxDist. For any given value of maxDist, a higher value of 'speed' will make the light cycle faster.
Ah okay. I tried it in an empty scene. THAT'S essentially how I want it to work... Still, it doesn't work within the confines of my script. WITHOUT an if statement for the button press, it works fine pulsing automatically. Whenever I use an if statement to wait for a button, however, the range is random between the range and it stays constantly in that range unless I press my button again. Why is that?
Here's my code as it is.
using UnityEngine;
using System.Collections;
public class LightFlashC : $$anonymous$$onoBehaviour {
public float lerpTime = 0.5f;
private float i = 0;
public int maxDist = 5;
public float speed = 40.0f;
// Use this for initialization
void Start () {
light.range = 0;
}
// Update is called once per frame
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)) {
light.range = $$anonymous$$athf.PingPong(Time.time * speed, maxDist);
i+= Time.deltaTime;
}
}
}
I'm not sure how you want this to work. If you only want it to flash when the button is down, you need to use Input.Get$$anonymous$$ey() rather than Input.Get$$anonymous$$eyDown(). Input.Get$$anonymous$$eyDown() only returns true for the single frame it the key is down. In addition, you probably want to use your own timer. The issue with using Time.time and start and stopping is that Time.time continues to march one so when you restart your flasher, you don't know where it is in the cycle. You may want something like:
using UnityEngine;
using System.Collections;
public class FlashLightC : $$anonymous$$onoBehaviour {
public float maxDist = 5.0f;
public float speed = 40.0f;
private float timer = 0.0f;
void Start () {
light.range = 0;
}
void Update () {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) {
light.range = $$anonymous$$athf.PingPong(timer * speed, maxDist);
timer += Time.deltaTime;
}
}
}
Hmm... After all these changes, it doesn't seem to fix things. On button press, the light is still static and unchanging until I press the button again. It doesn't animate itself back to 0 like it would without the if statement... =/