U
U
Uncle Bogdan2021-10-05 17:08:28
Unity
Uncle Bogdan, 2021-10-05 17:08:28

How can I change the color of a 3d object in animation?

Such a problem. There is a 3d model of the character. I want it to glow red when it takes damage. I wanted to make an animation, but the object does not have the usual 2d Color field.

How to do it?

615c5c58ef7be296315849.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NoNameDeveloper, 2021-10-05
@NoNameDeveloper

The material must have a _Color field.
You will create a script that will change the color value through "material.SetColor ()" (if I'm not mistaken).

public class MyCode 
{
  [SerializedField] private Color _color = Color.red;

  // Соединяешь материал что используется в MeshRenderer (из ресурсы).
  [SerializedField] private Material _mat; 

  public void TakeDamage()
  {
    // ...
    // блаблабла
    // ...

    StartCoroutine(ChangeColor());
  }

  private IEnumerator ChangeColor()
  {
    float speed = 5;

    // Изменение с белого на красный
    for(float t = 0; t < 1; t += Time.deltaTime * speed)
    {
      _mat.SetColor("_Color",  Color.Lerp(Color.white, _color, t));
      yield return null;
    }

    // И обратно.
    for(float t = 0; t < 1; t += Time.deltaTime * speed)
    {
      _mat.SetColor("_Color",  Color.Lerp(_color, Color.white, t));
      yield return null;
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question