How to change material color for certain time?
Hello, I have a 2D hierarchical stickman character, when it collides with gameobject with tag enemy I want it to change color to red but just for small period of time (for example 0.25 sec.) and then turn it back black. I have no idea how to make it work, I tried to make it with color lerp but it doesn't worked. This is my code, if can anyone make it work i would be very thankful :)
void Start() {
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>()) {
sr.material.color = Color.black;
gameObject.GetComponent<Renderer> ().material.color = Color.black;
}
}
void OnCollisionEnter2D (Collision2D coll) {
if (coll.gameObject.tag == "enemy") {
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>()) {
sr.material.color = Color.red;
gameObject.GetComponent<Renderer> ().material.color = Color.red;
//now I need to make it red just for specified time and then again black...
}
}
}
Answer by Hellium · Apr 26, 2018 at 03:27 PM
You will need coroutines to do so.
Either create a dedicated function:
System.Collections.IEnumerator OnCollisionEnter2D( Collision2D coll )
{
if ( coll.gameObject.tag == "enemy" )
{
foreach ( SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>() )
{
StartCoroutine( SwitchColor( gameObject.GetComponent<Renderer>(), Color.red ) ) ;
StartCoroutine( SwitchColor( sr, Color.red ) ) ;
}
}
}
System.Collections.IEnumerator SwitchColor( Renderer renderer, Color newColor )
{
Color color = renderer.material.color;
renderer.material.color = newColor ;
yield return new WaitForSeconds( 2 );
renderer.material.color = color;
}
Or make OnCollisionEnter
a coroutine (be carefull, this method does not apply to every MonoBehaviour functions)
System.Collections.IEnumerator OnCollisionEnter2D( Collision2D coll )
{
if ( coll.gameObject.tag == "enemy" )
{
Renderer renderer = gameObject.GetComponent<Renderer>();
SpriteRenderer[] spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
// Turn red
foreach ( SpriteRenderer sr in spriteRenderers )
{
sr.material.color = Color.red;
}
renderer.material.color = Color.red;
// Wait
yield return new WaitForSeconds( 2 );
// Turn black
foreach ( SpriteRenderer sr in spriteRenderers )
{
sr.material.color = Color.black;
}
renderer.material.color = Color.black;
}
}
I need to make it with Time.deltaTime or Time.time and I don't know how because nothing I try is working...
You mean you want a smooth transition between the colors?
I wanted to make that delay with Time.deltaTime because none of options you showed me worked but after my brother fixed something, the second way started working properly so thanks a lot :)
Your answer
Follow this Question
Related Questions
How to change the color of an object when it collides with another object? 2 Answers
Help Change Color of Object on Collide 1 Answer
Changing light color between players when they collide (BOLT) 0 Answers
Assigning a different material to different objects 2 Answers
How can I detect the color that the ball has collided with? 3 Answers