Color.Lerp over HP
Hello. I'm trying to create a floor that heats up by being in contact with certain objects. and it cools when coming in contact with others.
The thing is that I want to show visually by the color of the floor if it is cold or hot. Because when it reaches its maximum temperature the floor falls.
CODE: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EstadosPlataforma : MonoBehaviour {
//Controller controller;
public float grados;
Rigidbody rgb;
public bool Helada, Ardiendo;
public Color StartColor;
public Color EndColorBlue;
public Color EndColorRed;
public GameObject Heat;
public GameObject Frio;
// Use this for initialization
void Start () {
grados = 0; //---DEGREES--//
rgb = GetComponent<Rigidbody> ();
Helada = false; //--FROZEN FLOOR--//
Ardiendo = false; //--BURNING FLOOR--//
}
// Update is called once per frame
void Update () {
if (grados >= 100f) {
Ardiendo = true;
} else {
Ardiendo = false;
}
if (grados <= -100f) {
Helada = true;
transform.gameObject.tag = "PlataformaHelada";
Frio.GetComponent<AudioSource>().enabled = true;
} else {
Helada = false;
transform.gameObject.tag = "Plataforma";
}
if (Ardiendo) {
rgb.constraints = RigidbodyConstraints.None;
Heat.GetComponent<AudioSource>().enabled = true;
Destroy (gameObject, 2);
}
}
void OnCollisionStay (Collision c) {
if (c.transform.tag == "Hielo" && grados > -100f) {
grados -= 3.5f * Time.deltaTime;
}
if (c.transform.tag == "Fuego" && grados < 100f) {
grados += 3.5f * Time.deltaTime;
}
if (c.transform.tag == "Golem" && grados <= 100f) {
grados += 1.5f * Time.deltaTime;
} else {
grados += 0f * Time.deltaTime;
}
}
}
Answer by Harinezumi · Jan 28, 2018 at 11:39 PM
To solve this, you should set the color of the material you are using:
private Renderer ownRenderer; // private member variable to store the renderer
// add to Start(), or even better, to Awake()
ownRenderer = GetComponent<Renderer>();
// in Update(), when Ardiendo = true
ownRenderer.material.color = EndColorRed;
// in Update(), when Helado = true
ownRenderer.material.color = EndColorBlue;
// in Update(), when neither Helado nor Ardiendo is true
ownRenderer.material.color = StartColor;
Note that this will only change the main color of one renderer. If your platform model consists of multiple Renderers or has a complex material, then you will need a more complex setup (for example with Renderer[] ownRenderers = GetComponentsInChildren<Renderer>();
and/or cycle through ownRenderer.materials
).
Additionally, you may want to use Color.Lerp(startColor, endColor, Mathf.Abs(grados / 100));
to transition the color over time (post a comment if you need a help with this ;) ).