How to gradually transform from "color A" to "color B" in unity

principle

In Unity, you can use the color interpolation function Lerp to achieve the effect of gradually changing from color A to color B. This function requires three parameters: starting color A, target color B, and an interpolation value t between 0 and 1, indicating the degree of change. By updating the interpolation value t at each frame, a smooth color gradient effect can be achieved.

code

public Color colorA;
public Color colorB;
public float duration = 2.0f;
private float t = 0;

void Update() {
t += Time.deltaTime / duration;
GetComponent<Renderer>().material.color = Color.Lerp(colorA, colorB, t);
}


In the above code, we use a variable t to represent the interpolation value to achieve the color gradient. In the Update function, we update the value of t, then use the Color.Lerp function to interpolate the two colors according to the interpolation value t, and finally assign the interpolated color to the material color of the object to achieve a color gradient effect.

Color changes, what can it be used for?

Color changes can be used to create many interesting effects, such as achieving a smooth transition from one state to another in a game, creating dynamic background colors, or giving UI elements a gradient effect. In addition, color gradient is also an important means to achieve some special effects, such as atmosphere changes, flickering effects, etc.

Other lerps

In addition to the color interpolation function Lerp, there are many other interpolation functions available in Unity. For example, Vector3.Lerp can be used to achieve a smooth movement effect from one position to another, and Quaternion.Lerp can be used to achieve a smooth transition effect from one rotation state to another. The usage of these interpolation functions is similar. You need to specify the starting value, target value and interpolation value, and then update the interpolation value at each frame to achieve a smooth transition effect.

smooth movement

The following is a code example that uses the Vector3.Lerp function to achieve smooth movement:

public Vector3 positionA;
public Vector3 positionB;
public float duration = 2.0f;
private float t = 0;

void Update() {
t += Time.deltaTime / duration;
transform.position = Vector3.Lerp(positionA, positionB, t);
}


In this example, we use the Vector3.Lerp function to achieve a smooth movement from one position to another. Similar to the color interpolation function Lerp, we need to specify the starting position positionA, the target position positionB and the interpolation value t, and then update the interpolation value in each frame to achieve a smooth movement effect. Finally, we assign the interpolated position to the object's transform.position to achieve smooth movement.

Smooth rotation

public Quaternion rotationA;
public Quaternion rotationB;
public float duration = 2.0f;
private float t = 0;

void Update() {
t += Time.deltaTime / duration;
transform.rotation = Quaternion.Lerp(rotationA, rotationB, t);
}


Here is a code example that uses the Quaternion.Lerp function to achieve smooth rotation. Similar to the previous example, we need to specify the starting rotation state rotationA, the target rotation state rotationB and the interpolation value t, and then update the interpolation value at each frame to achieve a smooth rotation effect.

6a248530a0cf0fddf028cfe8293354f7.jpeg

Guess you like

Origin blog.csdn.net/shguxudong11/article/details/129387283