Moving Platforms in Unity

Objective: Implement a moving platform in Unity

To start off with this we will set the camera to follow the Player. We can easily do this by making the camera a child object of the Player. To make the moving platform more visable in the inspector we can rename that specific platform to moving platform.

Create a script called MovingPlatform and attach it to the moving platform object. We then create 2 empty objects and call them point a and b. We set the postion of the platform start point to a and the position we want the platform to move to point b.

To make the movement between positions possible, we create 2 transform objects called targetA and targetB, each assigned to its relative point. We need a speed variable of type float for how fast the platform moves. To get to platform moving between points we set a private bool called switching.

If switching is true the platform moves towards point b and vise versa.

Finally we need the Player to move with the platform when its moving. We start this process by adding a new box collider to the moving platform and set the center slightly above the platform so it can be used as a trigger.

We then set up an OnTriggerEnter on the moving player checking if the other tag is the Player and if so, setting the Players transform.parent to the moving platform making the Player a child of the moving platform which will move the Player with the platform. We now have an issue where the Player is jittery on the platform.

To fix this we change the Update method to a FixedUpdate which uses the Physics Update which is a consistent timestamp and removes the jitter. Now that the Player is moving smoothly with the platform we need to remove the Player as a child when the Player leaves the platform. For this we use the OnTriggerExit and set the Players transform.parent to null. A material was added to the moving platform to make it easier to identify in the scene.

To modularize the platform we create an empty object called Moving_Platform and place the platform and points inside it and zero all the positions. We then prefab the full object and now when we need a vertical or horizontal platform or even a diagonally moving platform we only need to set the position of point b.

In the next post we will look at Player lives.





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

Recommended from Medium

Using intelligent semantic technologies in the process of automation and optimization of…

List all symbolic links in a directory

Project Lightbulb

Bath in Winters Codechef Solution | MARCH CHALLENGE

Bath in Winters Codechef Solution | MARCH CHALLENGE

Architecture Made Easy: 4 Simple Ways you can Perfect your Code Design

Automate downloading files from SFTP

Code for All Dispatch — December 2019 Edition

CSS Flexbox Guide

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
Simon Leen

Simon Leen


More from Medium

Creating a UI Shield Strength indicator

Adding Challenge — Limited Ammo Count

How to Add Custom Post Processing Files to Individual Cameras in Cinemachine

Ledge Grab System: Part II