Nerd For Tech
Published in

Nerd For Tech

Moving Platforms in Unity

With any platform style games, we need to look at creating some type of movement for the platforms so that it has the player guess which time is the best for them to jump. To start, we need to create a new platform to work with and create waypoints that our platform will be travelling between:

With our platforms organized within a new empty objects, we will place our waypoints within their own section but labelled for each of the moving platforms. This is because if we place it as a child of the moving platform, the platform will move on forever as when it moves, the waypoint moves.
From here, we will create a new script and start to work on our code needed for the movement:

What we have done here is assigned our platforms to start at the first position, and from there assigned a Vector3.Movetowards directive for the platform. In order to not have our platform just bounce in 1 spot because it wants to go back to point a, we have to create a bool. In order to have this bool change it’s value, we will tell Unity that once it reaches it’s destination, we want it to alter it’s status so that we can switch back in the other direction. With this in place, we can see how it works within our game:

With our platform now moving back and forth, let’s see how it interacts with our player when it jumps onto the platform:

Our player properly lands and can move along the platform, however we need to move the player ourselves to stay on the platform. In this case, we will want to have our player move with the platform without having to do it, so let’s see how we can figure that out.
What we will start with is creating a box collider that will expand a bit further above our platforms:

From here, we are going to create an OnTriggerEnter script:

What we are able to do with the script is move our player to become a child of our platform, allowing us to move at the same rate as our player:

As we can see above however, we need to create a method to have our player disconnect from having the moving platform as a parent:

With this void, we will have our player leave the platform, allowing us to move on our own and not be dragged to unison with the platforms:

With moving platforms finished up, we can now look towards adding in a lives system so that we don’t have to constantly restart or replace our player object whenever we miss a jump.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Rethinking (Collective) Code Ownership — Avoid Diffusion of Responsibility

Why business-driven development must be above the choice tech stack, architecture and tools

The Haiku Approach to Writing Software

The importance of using good coding practices

Working progress tree by scope.ink

Continuous Deployment of server side applications at Dehaat (Part I)

How Software Talks to Other Software: APIs Explained for Humans

On Agile, Trello, and the ‘dryness’ of ideas

Key to a Healthy Android Project

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
Jordan Evans

Jordan Evans

More from Medium

2.5D P1 Framework: Complete!

Intro Physics with Unity

Building A Stealth Game — Creating Waypoints For Your AI Guards

Making a RTS game #49: Optimisation tips & tricks (Unity/C#)