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);        

  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;
  • 3
    \$\begingroup\$ 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? \$\endgroup\$ – Felsir Jul 31 '15 at 8:13
  • \$\begingroup\$ Make zone Z a trigger and on the entrance it makes the movement speed smaller. \$\endgroup\$ – Vadim Tatarnikov Jul 31 '15 at 12:47
  • \$\begingroup\$ 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 \$\endgroup\$ – 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.

enter image description here

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;


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

enter image description here

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)

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

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.