- Home /
How can I make my players change colors when they collide?
The game is a war between two players who are spheres, and when they collide with each other, I want them to change to red and then fade back to their original color. I've used this code for my walls to change colors: using UnityEngine; using System.Collections;
public class ColorChan : MonoBehaviour {
bool toYellow, toRed;
float lerpValue, lerpDuration;
void Start()
{
toYellow = false;
toRed = false;
lerpValue = 0f;
lerpDuration = 1f;
}
void Update()
{
if (toRed)
{
//change color to red
gameObject.GetComponent<Renderer>().material.color = Color.red;
toRed = false;
}
else if (toYellow)
{
// fade color to green
if (lerpValue < 1)
{
lerpValue += Time.deltaTime / lerpDuration;
gameObject.GetComponent<Renderer>().material.color = Color.Lerp(Color.red, Color.yellow, lerpValue);
}
else
{
toYellow = false;
lerpValue = 0f;
}
}
}
void OnCollisionStay()
{
toRed = true;
}
void OnCollisionExit()
{
toYellow = true;
}
} but when I applied it to my players I noticed that because of the plane they are on, the color stays the changed color, please help me understand how to if I can apply this to my players only when they collide with each other.
You can add a collision as a parameter for OnCollision methods. Through this you can check the tag of the collision, among other things.
http://docs.unity3d.com/ScriptReference/Collider.html
http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
@Namey5, I checked out the links you gave but I'm still having trouble wrapping my head around this. I still pretty new at this, so if you could explain more of what you meant it would be very appreciated.
It works like in Johnz' answer. You add a collision or a collider as a parameter to the OnCollision functions. EG;
bool toYellow, toRed;
float lerpValue, lerpDuration;
void Start()
{
toYellow = false;
toRed = false;
lerpValue = 0f;
lerpDuration = 1f;
}
void Update()
{
if (toRed)
{
//change color to red
gameObject.GetComponent<Renderer>().material.color = Color.red;
toRed = false;
}
else if (toYellow)
{
// fade color to green
if (lerpValue < 1)
{
lerpValue += Time.deltaTime / lerpDuration;
gameObject.GetComponent<Renderer>().material.color = Color.Lerp(Color.red, Color.yellow, lerpValue);
}
else
{
toYellow = false;
lerpValue = 0f;
}
}
}
void OnCollisionStay (Collider col)
{
if (col.tag == "Player")
toRed = true;
}
void OnCollisionExit (Collider col)
{
if (toRed == true)
toYellow = true;
}
$$anonymous$$ake sure your player's tags are set to "Player" (create one if there isn't already).
Answer by Johnz1234 · May 19, 2016 at 09:34 AM
You can use OnTriggerEnter every time player touches some object color it's changing to red then affter few sec it's back to normal color :D
OnTriggerEnter(Collider other) { if(other.gameObject.tag == "Enemy") { GetComponent().color = Color.red; StartCorutine("FadeBack"); } } void FadeBack() { yeild return new WaitForSeconds(1); GetComponent().color = Color.black; }
Your answer