How do I make an object slow down to a stop?

I am currently Trying to make my train go to a stop. The character is on a moving train and I want him to pass a certain point and this will make the train slow down and stop. I currently have a stationary train and a moving background to give the illusion that the train is moving. How can I make the background slowly stop after a certain point? Any suggestions? I assumed a simple for loop would be able to do this but can not figure it out. This is my code for it.

var speed:double; function Start () {


function Update () { gameObject.transform.position.x-=speed; } function OnTriggerEnter () { gameObject.transform.position.x-=0; }

more ▼

asked Oct 21, 2012 at 10:20 PM

avatar image

1 3 2 2

unityGEMS.com will help

Oct 22, 2012 at 07:44 AM Fattie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You could use Slerp function: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Slerp.html

or SmoothDamp


and apply it to your position vector. I would do that in the Update function

more ▼

answered Oct 22, 2012 at 08:55 AM

avatar image

173 17 14 18

How can I use these to help me? I havent been able to use either of these to work

Oct 23, 2012 at 11:21 PM bbergeron

Slerp make is go in a circle and lerp is only one second long and I need it to be a lot higher than that

Oct 23, 2012 at 11:23 PM bbergeron

"lerp is only one second long"

No, lerp is only Linear Interpolation. It has nothing to do with time, it takes a start value, an end value, and a percentage.

Oct 23, 2012 at 11:57 PM Kiloblargh

You haven't shown much code, but I think this is what you need:

float decelTime = 10; // 10 seconds to decelerate, for example float currentDecelTime = 0; bool decelerating = false; Vector3 speed; //I always think it's best to use Vectors instead of floats.

Update() { if(decelerating){ float interpolatingFactor = currentDecelTime/decelTime; Vector3 move = Vector3.Slerp(speed, Vector3.zero, interpolatingFactor);

transform.position -= move; currentDecelTime += Time.deltaTime; } }

OnTriggerEnter() { currentDecelTime = 0; decelerating = true; }

Translate that into JS or C# and it should be running (I made quite a few syntax errors, but I'm doing this with a tablet and it's not very comfortable)

Oct 23, 2012 at 11:58 PM Hannibalov

you MAN! many thanks +++

void CameraFocus() { if (LeftMouse_CLICK) { focusedObject = GetClickedGameObject(); } if (focusedObject) { Debug.Log("focusedObject:"+focusedObject); if (LeftMouse_UP) { cameraOBJ.transform.position = Vector3.SmoothDamp(cameraOBJ.transform.position, focusedObject.transform.position, ref velocity, FocusTime);

Mar 21, 2013 at 10:19 PM sonapax
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 21, 2012 at 10:20 PM

Seen: 4155 times

Last Updated: Mar 21, 2013 at 10:19 PM