Moving Platform

How to create a moving platform in Unity

In this article we will create a moving platform for our platformer game.

As always there are various way to do the same thing in programming this is just one of the many.

We will be using Vector3.MoveTowards.

Moving Platform Script

We use game objects with just transform components as a reference. Our moving platform will be moving between these points. We assign these points from the Inspector.

In the Hierarchy
Assign the transforms through the Inspector

Also we define a speed value with which we can adjust the speed our moving platform moves.

In Update() we move our platform towards our current target.
We also measure the distance from the current platform position to the target points A and B. If it reaches a certain point it will switch the target to the other point. This way the platform will ping pong between these points indefinitely.

[SerializeField] private Transform _pointA, _pointB;
private Transform _target;
private float _speed = 1f;

void Start()
{
_target = _pointA;
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position,_target.position, _speed * Time.deltaTime);
if (Vector3.Distance(transform.position, _pointA.position) < 0.001f)
{
_target = _pointB;
}
else if (Vector3.Distance(transform.position, _pointB.position) < 0.001f)
{
_target = _pointA;
}
}

--

--

--

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What’s Better? Anaconda or Python Virtualenv

How to deal with Cache Stampedes

Dependency Injection in Nutshell with C#

How to Build a ChatOps-Bot with Slack and Kubernetes

Managing Auth0 Infrastructure With Terraform and Terragrunt

Notion is the New Hammer

Functional Test Refactoring

Create and connect a PSQL database with docker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tristan Engel

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

More from Medium

Kubernetes the hard way on bare metal/VMs — Setting up DNS

How share your vnc remote session on LAN network with low rate of latency in Linux

Modular Powerup Systems?

Finding .Net Assemblies