- Home /
re use a charged ability that isn't at 100 percent?
I hope I can explain this correctly. But I want to have this slow motion feature represented by a progress bar.
So far I've got it working nicely where if you press the slow motion button time scales to the fraction I want it too, and then the bar starts draining down to 0...aftre it hits zero , timeScale goes back to 1 again (regular speed) and that bar at the bottom starts to slowly fill back up again to full representing how much "slow motion counter you currently have" it's a float from 0-1.
perfect!
but here's the problem;
what if the player wants to use that slow motion again, and it's not reset back to full? The desired effect I want is to have it drain whatever remains in the bar back down to zero again.
However, in order to do this I need to stop the current coroutine/function that is currently wanting to lerp the bar back to full...but also find the value of that bar (0-1) and convert that to the slowMotionDuration percent (the value slowMotion lasts in seconds, which in my case is 5)
what I currently have;
function SlowMotion ()
{
var t: float = 0.0f;
var w: float = 0.0f;
var r: float = 0.0f;
var br: float = 0.0f;
//lerps timescale to .3
while(t < 1)
{
timeScale = Time.timeScale;
t += Time.deltaTime / slowMotionSpeed;
Time.timeScale = Mathf.Lerp (1, .3, t);
yield;
}
Time.timeScale = .3;
//drains slow mo bar to 0
while (w < 1)
{
w += Time.deltaTime / timeLeft;
slowMotionSliderScript.sliderValue = Mathf.Lerp (1, 0, w);
yield;
}
//lerps timescale back to 1
while(r < 1)
{
timeScale = Time.timeScale;
r += Time.deltaTime / slowMotionSpeed;
Time.timeScale = Mathf.Lerp (.3, 1, r);
yield;
}
Time.timeScale = 1;
//slowly resets bar
while (br < 1)
{
br += Time.deltaTime / 30;
slowMotionSliderScript.sliderValue = Mathf.Lerp (0, 1, br);
yield;
}
}
Answer by meat5000 · Jun 22, 2014 at 10:14 AM
The bar itself is a display of something else. You shouldn't read off it back in to the system.
Have your bar read from the float, not the other way round. You are overcomplicating the mechanics of the system otherwise.
As long as
//Just an illustration
if(slowmoFloat <= 0.0)
{
//Stop the effect
//Flag recharge
}
There should be no difficulty.
Even if I have the bar reading from a float variable, I'm going to have 1 co routine wanting to refill that bar, and another co routine wanting to empty it, should the player repress the slow motion button...
Why do you need to do this via coroutine? It's just moving a couple of textures around, is it not?
There are only 3 possible states. Filling, Emptying and Stopped. If you dont want it to stop thats even easier; they are opposite conditions so are easily handled by if-else.
I have a feeling that your difficulty arrives from overzealous usage of the while loop.
Look at Health bar examples. Its the same. You can drain or refill health and the bar reads the values and repositions the textures. Simple, no?