using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColors : MonoBehaviour
{
public List<Transform> objectsToRotate = new List<Transform>();
public float rotationSpeed;
public Light[] lights;
public Material material;
// 0,31,191
// 255,0,0
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < objectsToRotate.Count; i++)
{
objectsToRotate[i].Rotate(new Vector3(Time.deltaTime * rotationSpeed, 0, 0));
}
var c = Color.Lerp(new Color(255, 0, 0), new Color(0, 31, 191), 0.5f);
material.SetColor("_EmissionColor", c);
}
}
If I will take the part :
new Color(255, 0, 0)
and put it in the SetColor :
material.SetColor("_EmissionColor", new Color(255, 0, 0));
Then it will change the color but I want that it will lerp between the two colors all the time on half the way when rotating. So I tried this :
var c = Color.Lerp(new Color(255, 0, 0), new Color(0, 31, 191), 0.5f);
but then when I add the variable c to the SetColor the result is just white color.
The RBGA values of a Color instance must be between 0 and 1 instead of 0 and 255.
If you absolutely need to use values between 0 and 255, use Color32 instead.
This is almost working. The problem is that the time variable vlaue is never exactly 0 and never exactly 1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColors : MonoBehaviour
{
public List<Transform> objectsToRotate = new List<Transform>();
public float rotationSpeed;
public Light[] lights;
public Material material;
private float time = 0;
// 0,31,191
// 255,0,0
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < objectsToRotate.Count; i++)
{
objectsToRotate[i].Rotate(new Vector3(Time.deltaTime * rotationSpeed, 0, 0));
}
if (time >= 1f)
{
time -= Time.deltaTime * 0.1f;
}
if(time < 0.01f)
{
time += Time.deltaTime * 0.1f;
}
var c = Color.Lerp(new Color(255, 0, 0), new Color(0, 31, 191), time);
material.SetColor("_EmissionColor", c);
}
}