My code is not working properly. I want my Player to change color overtime when it is located in a Zone. It does work when I am in the first Zone, but when i go to another Zone, which has the same tag as the first one, It continues the Lerp where it ended in the first Zone.
So basically I am asking, if there is any way to reset to the StartColor everytime I am in a Zone? Or do you have any other sollutions?
Thanks a lot guys!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColorOnTrigger : MonoBehaviour
{
public float speedIn = 1.0f;
public float speedOut = 1.0f;
public Color startColor;
public Color endColor;
float startTime;
// Start is called before the first frame update
void Start()
{
startTime = Time.time;
}
void OnTriggerStay(Collider col){
if (col.gameObject.tag == "zone1"){
float t = (Time.time - startTime) * speedIn;
GetComponent<Renderer>().material.SetColor("_BaseColor", Color.Lerp(startColor, endColor, t));
}
}
void OnTriggerExit(Collider col){
if (col.gameObject.tag == "zone1"){
float t = (Time.time - startTime) * speedOut;
GetComponent<Renderer>().material.SetColor("_BaseColor", Color.Lerp(endColor, startColor, t));
}
}
}