- Home /
Trying to get object to blink (speed acording to timer)
I'm trying to get a cube object to blink by using renderer.enabled, and have the speed it blinks be measured by a timer. For example the red coins in Mario games. I want it to slowly start to blink faster as it gets closer to the time limit i have set up. Anyone know the best way to go about it?
Answer by robertbu · Apr 02, 2014 at 04:11 PM
'Best is always open to interpretation, but here is some starter code:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public float time = 30.0f;
public float maxInterval = 1.2f;
public float minInterval = 0.2f;
private float interval = 1.0f;
private float timer = 30.0f;
void Start() {
timer = time;
}
void Update () {
interval = minInterval + timer / time * (maxInterval - minInterval);
timer -= Time.deltaTime;
if (timer < 0.0f) timer = 0.0f;
renderer.enabled = Mathf.PingPong(Time.time, interval) > (interval / 2.0f);
}
}
holy math Batman, that's some pretty intense 'starter' code ;)
just to mention, the OP has tagged C#
Thanks for the help to everyone who responded ! Got it to work well.
Transform blinkOfTransform;
transform.position = blinkOfTransform.position;
or
use rigidbody force, or use vector3 forward with either + or - on 25 speed, etc, but transform wil lbe much beter jsut find the code
Answer by patrik-org · Apr 02, 2014 at 05:05 PM
Perhaps something like:
blinkSpeed = startSpeed + totalSpeedIncrease * timeElapsed / maxTimeElapsed; // blink speeds measured in blinks per second
t += Time.deltaTime * blinkSpeed;
if (t >= 1)
{
t = 0;
// switch renderer.enabled
}