- Home /
Making a model flash a certain color when shot
So I have a Player character that shoots a prefab projectile at an enemy prefab. What would be the best way to have that prefab flash a certain color once the projectile and enemy collide. It seems like it would be a simple solution, but I'm not sure how to implement it.
Answer by Eric5h5 · Mar 24, 2010 at 05:50 PM
Change the renderer.material.color briefly; something like
OnCollisionEnter () {
renderer.material.color = collideColor;
yield WaitForSeconds(.5);
renderer.material.color = normalColor;
}
This works fine even if the prefab does have a texture. The entire texture will be tinted by the color of the material.
Answer by IRobb · Jul 07, 2014 at 01:39 AM
I have made this for my game. It's a coroutine called from OnTriggerEnter. MainRenderer is the renderer associated with the object. It disables the material, then it changes color to white. This code is from a bigger class where color and material are saved before.
IEnumerator collideFlash() {
Material m = this.mainRenderer.material;
Color32 c = this.mainRenderer.material.color;
this.mainRenderer.material = null;
this.mainRenderer.material.color = Color.white;
yield return new WaitForSeconds(0.1f);
this.mainRenderer.material = m;
this.mainRenderer.material.color = c;
}
Answer by Binxalot · Jan 23, 2014 at 07:51 PM
quick note for anyone using this. The code above works for the diffuse shader, not the mobile diffuse shader, also, it affects the material across all models in your scene so you'll want to be careful if you have a bunch of clones running around, those will blink as well unless you change it so they have unique materials assigned. This works perfectly though.
Your answer
Follow this Question
Related Questions
Make enemy Flash when Hit 3 Answers
How can I load a scene when the enemy hits the player? 5 Answers
Blood on weapon. 1 Answer
Raycast gun 2 Answers
Show Enemy Name On UI Canvas? 1 Answer