Slow object with time

Hi,

I need to slow down several objects according to their speed: the faster the speed, the longer it will take to slow down.
I tried with a Lerp function but I don’t manage to make it work…

velocityRing = Mathf.Lerp(velocityRing, 0, Time.deltaTime);

Any idea?

Nicolas

Mathf.Lerp() doesnt work the way you think. The third argument is effectively the percent along the lerp you want. so you’ll want to have something like a counter variable or store the time and compare to the new time. My suggestion would be something like:

Have this somewhere else in the class(not in a function)

float timer = .0f;
float velocityRing = startSpeed;

Then in your function have:

timer += Time.deltaTime;
velocityRing = Mathf.Lerp(startSpeed, 0, timer);

Assuming that’s called in Update() it would slow to 0 over exactly one second. To change the speed it decelerates multiply Time.deltaTime; Like so:

timer += Time.deltaTime * .5f; //takes 2 seconds to decelerate
timer += Time.deltaTime * 2.0f; //takes .5 seconds to decelerate

There are many different ways of doing this depending on your goal. Here is one way. In a new scene, create a game object, put it to the left edge of the Game window, and add this script.

#pragma strict

var velocity = Vector3(6.0, 0.0, 0.0);
var slowFactor = .5;  // needs be between 0 and 1

function Update() { 
	transform.Translate(velocity * Time.deltaTime);
	velocity = velocity * (1.0 - (slowFactor * Time.deltaTime));
	if (velocity.magnitude < 0.1)
	  velocity = Vector3.zero;
}