0
Answered

Need help getting Color Lerp to work correctly

barskey 3 years ago updated by Lazlo Bonin (Lead Developer) 3 years ago 3

I have a HUD scene that is loaded using additive mode over my regular scene. In that HUD scene I have an image that I wish to use to flash the screen when my character gets hurt. In C# I had it working fine using the following code in the Update method:

damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);

In Bolt, I am trying to replicate this behavior. I have a State Machine (embedded) on the image as follows:

 It transitions to "Red Display" state with a Custom Event trigger. This is working correctly. The Red Display state looks like this (split in two images for clarity):


Yet instead of "Lerp"ing the color (in this case from my start color to clear), it is simply setting the color to something based on the T input, but not converging on the end color. Here is a sample output to the console:


The state machine is working as expected, and exits the state after my timer has elapsed, which is enough time needed to change the color. Does anyone have any advice, or might this be a bug?

Thanks in advance.

Steve

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
GOOD, I'M SATISFIED
Satisfaction mark by barskey 3 years ago

I find it hard to believe that your C# worked as described. You need to also have a start time and possibly end time, depending on what you're doing. The t value of lerp is basically a percentage, from 0 to 1. So at 1, it will be 100% to the new Color. You need to calculate the fraction of complete the lerp is for the t value. So if it takes 5 seconds to complete, then after 1 second, t = 0.2. You need to add the deltaTime to a variable (such as elapsedTime), then divide by the total time you want it to take.

The example below isn't for color lerp, but it should be useful none the less. Somebody asked about lerping position on Discord earlier, and this was the example I gave:

OK, that makes sense. Thanks for the thorough (and quick!) response. In retrospect, I'll bet my C# script just appeared to work because it was such a short period of time and the alpha value was very low, hence difficult to see the entire transition.

Answered

Glad you got this figured out!

As a side note, lerping workflows will become much easier in v.1.4 once tweening nodes are added :)