# Decelerate moving platform at targets

I'm trying to make a moving platform move at a constant speed and then when it is near the target start to slow down, I have trying to do this with Lerp, but I'm not satisfied with the results because of two reasons: it goes way too fast between targets, and way too slow near targets.

Also, when it reaches a target it immediately boosts off in the opposite direction. What I want to achieve is for the platform to move steadily between targets, decelerate near targets, and when reaching a target start to accelerate back to the steady speed.

What is the best way for achieving this? Lerp, Smoothstep, other? Thanks! (I'm using Unity)

My current solution:

 _dVeloctiy = _velocity * Time.fixedDeltaTime;
Vector3 newPosition = Vector3.Lerp(_rigidBody.position, currentTargetPosition, _dVeloctiy.magnitude);
_rigidBody.MovePosition(newPosition);

Vector3 dPosition = _rigidBody.position - currentTargetPosition;

if (dPosition.magnitude > 0.05f)
{
_isTurning = false;
}

if (dPosition.magnitude < 0.05f && !_isTurning)
{
currentTargetPosition = (currentTargetPosition == targetPosition1 ? targetPosition2 : targetPosition1);
_isTurning = true;
}

• So you have target A and platform B. Then there is the slowdown zone Z which is a distance from A. If distance A->B bigger than Z move a constant speed, and if it is smaller you want it to slowdown relative to the distance to A. So; the speed of the platform has a relation to Z, not the entire journey. Make your speed LERP while inside Z (unrelated to the direction) and while outside Z, use the constant speed? – Felsir Jul 31 '15 at 8:13
• Make zone Z a trigger and on the entrance it makes the movement speed smaller. – Vadim Tatarnikov Jul 31 '15 at 12:47
• You might give bias and gain a look. Lots of info on Google, but here's a link to start: blog.demofox.org/2012/09/24/bias-and-gain-are-your-friend – Alan Wolfe Aug 2 '15 at 5:02

Your best friend is probably to use Mathf.Sin, the platform will move at constant speed and decelerate when reaching either side.

using UnityEngine;

public class MovingPlatform : MonoBehaviour
{
private void Update()
{
var f = Mathf.Sin(Time.time);
var position = transform.position;
position.x = f;
transform.position = position;
}
}


Configuration:

I assume that it is initially placed at the center location between zones it has to reach (platform is the white cube) :

It will start by moving from the center, if you'd like it to start moving from an edge instead then use Mathf.Cos.

Last thing, specifying a longer distance should be pretty easy and I'll let you figure that out :D

Here's a web-app to let you play with sines and so on: https://www.desmos.com/calculator

(remember, use x * pi to normalize the sine)

• So simple, yet so good! TY! :) – Unresolved External Aug 1 '15 at 21:08
• Simplicity at its best, I was unsure at first but finally it works really well :D – Aybe Aug 1 '15 at 23:07