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.