- Home /
A Blinking Turret?
I set up a turret that shoots at the player and has a temperature meter that can change color when the player gets to close. How could I go about getting the temperature to change from green to a blinking red and yellow, when the player gets too close?
using System.Collections;
public class TurretTemperture : MonoBehaviour
{
public Transform Target;
private float distance;
private Quaternion rotation;
private float lookAtDistance = 10;
private float reactionTime = 2;
void Update ()
{
if(Target)
{
distance = Vector3.Distance(Target.position, transform.position);
if (distance < lookAtDistance)
{
lookAt();
}
if (distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
}
else
{
Target = null;
}
}
void lookAt ()
{
renderer.material.color = Color.red;
rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * reactionTime);
}
}
You're already checking your distance, so use it. See if target is close enough to blink. If it is, make it blink. When it's not, make it stop blinking and go back to normal color. Answer to your question is basically your question itself.
I don't actually know how to make it blink. I can lerp the color from green to red, but that's about all i've figure out so far.
Answer by Fox-Handler · Jun 26, 2014 at 11:35 AM
Ok here's something that works pretty well. Used an Coroutine to have the light on the turret platform blinking red and yellow when you get near it, and go back to green when you get far enough away. Doesn't work 100 percent, but it's not bad.
IEnumerator Blinking()
{
if(blink == true)
{
yield return new WaitForSeconds(.1f);
renderer.material.color = Color.red;
yield return new WaitForSeconds(.1f);
renderer.material.color = Color.yellow;
}
blink = false;
}
using System.Collections;
public class TurretTemperture : MonoBehaviour
{
public Transform Target;
private float distance;
private Quaternion rotation;
private float lookAtDistance = 10;
private float reactionTime = 2;
private float timer;
private bool blink;
void Start()
{
timer = Time.time;
}
void Update ()
{
if(Target)
{
distance = Vector3.Distance(Target.position, transform.position); //a line /vector
if (distance < lookAtDistance) // look at us
{
lookAt();
StartCoroutine("Blinking");
blink = true;
}
if (distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
}
else
{
Target = null;
}
}
void lookAt ()
{
//renderer.material.color = Color.red;
rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * reactionTime);
}
IEnumerator Blinking()
{
if(blink == true)
{
yield return new WaitForSeconds(.1f);
renderer.material.color = Color.red;
yield return new WaitForSeconds(.1f);
renderer.material.color = Color.yellow;
}
blink = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613152537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Material doesn't have a color property '_Color' 4 Answers
how to change to another object after button is pressed 1 Answer
Vertex colours - can they improve performance? Do they affect batching? 1 Answer
Unity3d 'red' not regconized by 'Color' 3 Answers