I’m trying to make it so what when you’re looking at an enemy and holding down the mouse button, you shoot a flashlight that slowly makes it heat up, and turn more and more red.
Then, I want to make it gradually go back to its normal colour when not exposed to the flashlight.
I’m having trouble with getting the colour to change. I haven’t managed to get it to change at all yet despite looking it up. I’m sure I’m missing something, but I don’t know what.
public float Intensity = 0;
Renderer rend;
public Color start;
public Color end;
void Update()
{
if (lookedAt == true)
{
...
if (Input.GetAxis("Fire1") == 1)
{
Color c = Vector4.MoveTowards(start, end, Time.deltaTime * 5);
print("shotAt");
rend.material.color = new Color(c.r, c.g, c.b, 3);
}
}
else
{
...
}
}
@Chum725 - It seems like you’re on the right track. First, make sure you initialize the Renderer component in the Start or Awake method. Then, you can use a Coroutine to handle the color changing smoothly in both directions (heating up and cooling down).
Something like this:
using UnityEngine;
public class EnemyColorChange : MonoBehaviour
{
public float intensity = 0;
Renderer rend;
public Color start;
public Color end;
private Coroutine colorChangeCoroutine;
void Start()
{
rend = GetComponent<Renderer>();
}
void Update()
{
if (lookedAt == true)
{
// ...
if (Input.GetAxis("Fire1") == 1)
{
if (colorChangeCoroutine != null) StopCoroutine(colorChangeCoroutine);
colorChangeCoroutine = StartCoroutine(ChangeColor(start, end, 5f));
}
}
else
{
if (colorChangeCoroutine != null) StopCoroutine(colorChangeCoroutine);
colorChangeCoroutine = StartCoroutine(ChangeColor(rend.material.color, start, 5f));
}
}
IEnumerator ChangeColor(Color fromColor, Color toColor, float duration)
{
float elapsedTime = 0;
while (elapsedTime < duration)
{
elapsedTime += Time.deltaTime;
float t = elapsedTime / duration;
rend.material.color = Color.Lerp(fromColor, toColor, t);
yield return null;
}
}
}
In this updated script, I’ve added a Coroutine named ChangeColor that takes in the initial color, the target color, and the duration for the color change. This Coroutine lerps the color smoothly over the specified duration. In the Update() method, I’ve used the Coroutine to change the color when the enemy is looked at and the fire button is pressed, and to change the color back to its initial state when the conditions aren’t met.
Let me know if this gets you closer to an answer.