1
\$\begingroup\$

I'm trying to slow down a object over different distances in the same time, say 3 seconds.

My slow down code is.

function lerpSpeed (speed: float, lerpSpeedTime: float)
 {
     incomingFlightSpeed = flightPathScript.flightSpeed;
 
     i = 0;
     while(i < 1)
     {
         i += Time.deltaTime/lerpSpeedTime;
         i = Mathf.Clamp(i, 0, 1);
 
         flightPathScript.flightSpeed = Mathf.Lerp(incomingFlightSpeed, speed, i);
 
         yield;
     }
 }

So I call it with.

 waypointScript.lerpSpeed(1,(totaFlighpointsDistance/flightSpeed));

In the picture below I've draw a bezier curve and at the last 8 flight points (the red and yellow spheres) I want to start the slow down from the yellow sphere to the last red sphere.

I get the distance in between each of the last 8 flight points and total them up to give me the total distance, I understand to get the time it will take to travel that distance is time = total distance/speed, but as I'm slowing the speed in my function above this will not work.

And because every bezier curve I generate is different, some stretched out, with total distances sometimes ranging from 30 to 8.

So I'm a bit stuck here, thought I'd get this one, but unless I'm missing something simple I can't get my head around it, so any help would be greatly appreciated, thanks.

enter image description here

\$\endgroup\$
0
\$\begingroup\$

I've attached a few sample calculations below. The first two are linear burn downs, which isn't quite what you want, but it's good to see the math. Basically, each time you multiply the current speed by a reducing fraction that is a function of the number of stops you have. The last frame is always to Lerp to 0. All examples have two calculations, one from a starting speed of 30, one from a starting speed of 78.

Linear                                      
Speed   30  26.25   22.5    18.75   15  11.25   7.5 3.75    0   
Delta       -3.75   -3.75   -3.75   -3.75   -3.75   -3.75   -3.75   -3.75   
Math        7/8 6/7 5/6 4/5 3/4 2/3 1/2 zero out    

Speed   78  69.3    60.6    52  43.3    34.6    26  17.3    8.6 0
Delta       -8.7    -8.7    -8.6    -8.7    -8.7    -8.6    -8.7    -8.7    -8.6
Math        8/9 7/8 6/7 5/6 4/5 3/4 2/3 1/2 zero out

These sets are burn downs over time, each giving you the speed to Lerp to by the next point. The first is a four-second burn down, the second is a three-second burn down.

Multipled by 3/x (4 second burn down)                                       
Speed   30  11.25   4.21    1.58    0                   
Delta       -18.75  -7.04   -2.63   -1.58                   
Math        3/8 3/8 3/8 zero out                    

Speed   78  26  8.6 2.8 0                   
Delta       -52 -17.4   -5.8    -2.8                    
Math        3/9 3/9 3/9 zero out                    


Multipled by 2/x (3 second burn down)                                       
Speed   30  7.5 1.875   0                       
Delta       -22.5   -5.625  -1.875                      
Math        2/8 2/8 zero out                        

Speed   78  17.33   3.85    0                       
Delta       -60.67  -13.48  -3.85                       
Math        2/9 2/9 zero out    
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.