In the previous post “Randomizing color on demand” we build a flow to add random colors to our car and in this post, we will do a smooth color transition from the current color to the new random color.

In order to be able to do the transition from current color to new color we will use two vector variables, one for current color (Current_Color) and one for the new color (New_Color).

When the unit is spawned we set the current color variable to a new random value and when you hit the keyboard button we set the new color variable to a new random value.

In order to transition from one color to another we will loop from 0 to 100 and use a delay node to make sure that color change takes little bit of time. For each of these 100 steps, we do a linear interpolation and assign it to the material variable base color (base_color) so that the current color transitions to the new color.

At the end of the loop we are going to set the current color variable to the value of the new color

Continue from the post “Creating Camera switcher -2”or download project “here” and open Level “random_color_smooth_Transition_start”

1. In the random color group delete the random vector node

2. Delete the line between the Unit Spawned “Out” and Set Material Variable “In”

3. Delete the line between the Unit Spawned “Unit” and Get Unit Mesh “Unit”

4. Delete the Keyboard node

In order to simplify debug process, we are going to reduce the number of cars to 1 which we do by changing the value of Max_cars_Models to 1 instead of 7 in level flow.

1. Create a “Unit Spawned” node (Event – Unit Spawned)

2. Hook the “Unit” of “Unit Spawned” to the “Unit” of “Get Unit Mesh”

3. Create a “Set Vector Variable” node (Variable – Set Vector Variable)

4. Enter “Current_Color” in Name field

5. Create a “Random Unit Vector” node (Math – Random – Random Unit Vector)

6. Hook the “Out” of “Unit Spawned” to the “Set” of “Set Vector Variable”

7. Hook the “Value” of “Random Unit Vector” to the “Vector” of “Set Vector Variable”

8. Create a “Keyboard Button” node (Input- Keyboard Button)

9. Set the value for Button Name to “1”

10. Create a “Set Vector Variable” node (Variable – Set Vector Variable) &

11. Enter “New_Color” in Name field

12. Hook the “Pressed” of “Keyboard Button” to the “Set” of “Set Vector Variable”

13. Create a “Random Unit Vector” node (Math – Random – Random Unit Vector)

14. Hook the “Value” of “Random Unit Vector” to the “Vector” of “Set Vector Variable”

15. Create a “Delay” node (Flow Control – Delay)

16. Set the value for Time to “0.05”

17. Hook the “Out” of a “Set Vector Variable” to the “In” of “Delay”

18. Create a “Counter” node (Flow Control – Counter)

19. Set the values for the “Counter”

a. “Start value” 0

b. “Increment” to 1

20. Hook the “Out” of a “Delay” to the “Increase” of “Counter”

21. Hook the “Pressed” of “Keyboard Button” to the “Reset” of “Counter”

22. Create a “Numeric Data” node (Data – Numeric Data)

23. Set the value for the “Numeric Data” to 100

24. Create a “Compare Numerics” node (Flow Control – Compare Numerics)

25. Create a “Division” node (Math – Numeric – Division)

26. Hook the “Value” of “Numeric Data” to the

a. “B” of “Division”

b. “B” of “Compare Numeric”

27. Hook the “Value” of “Counter” to the

a. “A” of “Division”

b. “A” of “Compare Numeric”

28. Hook the “Changed” of “Counter” to the “Compare” of “Compare Numeric”

29. Create a “Get Vector Variable” node (Variable – Get Vector Variable)

30. Enter “New_Color” in Name field

31. Create a “Set Vector Variable” node (Variable – Set Vector Variable)

32. Enter “Current_Color” in Name field

33. Hook the “Vector” of “Get Vector Variable” to the “Vector” of “Set Vector Variable”

34. Hook the “Equal” of “Compare Numeric” to the “Set” of “Set Vector Variable”

35. Hook “Less or Equal” of “Compare Numeric” to the “In” of “Set Material Variable”

36. Hook the “Out” of “Set Material Variable” to the “In” of “Delay”

37. Create a “Lerp” node (Math – Vector- Lerp)

38. Create a “Get Vector Variable” node (Variable – Get Vector Variable)

39. Enter “Current_Color” in Name field

40. Hook the “Vector” of “Get Vector Variable” to the “From” of “Lerp”

41. Create a “Get Vector Variable” node (Variable – Get Vector Variable)

42. Enter “New_Color” in Name field

43. Hook the “Vector” of “Get Vector Variable” to the “To” of “Lerp”

44. Hook the “Value” of “Division” to the “T” of “Lerp”

45. Hook the “Value” of “Lerp” to the “Vector” of “Set Material Variable”

Once this is working as expected you can replicate this flow into the other cars by copying the entire flow and replacing the existing flow in the other car units and restore the Max_cars_Models to 7.

Run Level and hit 2 and 3 to switch between cameras and Hit 1 to smoothly change the car colors.

Screencast for “Random_color_smooth_Transition” and Project and open “random_color_smooth_Transition_Final”