- Home /
How to make enemies flash on hit
I want the enemy to become red for something like half a second when hit. I altready searched on Unity Answers but i didn't really understand...
Comment
Best Answer
Answer by webcam · Jun 19, 2017 at 04:09 PM
Hey mate, the easiest way to accomplish this is to change the material colour on your enemy. You can accomplish this by getting a reference to the Mesh Renderer.
public float flashTime;
Color origionalColor
public MeshRenderer renderer;
void Start()
{
origionalColor = renderer.color;
}
void FlashRed()
{
renderer.color = Color.red;
Invoke("ResetColor", flashTime);
}
void ResetColor()
{
renderer.color = origionalColor;
}
Answer by Habitablaba · Jun 19, 2017 at 04:32 PM
You could use coroutines to accomplish this effect (but also, to webcam's answer, change the material color).
IEnumerator FlashObject(MeshRenderer toFlash, Color originalColor, Color flashColor, float flashTime, float flashSpeed)
{
var flashingFor = 0;
var newColor = flashColor;
while(flashingFor < flashTime)
{
toFlash.color = newColor;
flashingFor += Time.deltaTime;
yield return new WaitForSecons(flashSpeed);
flashingFor += flashSpeed;
if(newColor == flashColor)
{
newColor = originalColor;
}
else
{
newColor = flashColor;
}
}
}