Working with moving Platforms
As you progress in most platformer games you will occasionally run across a platform that moves on it’s own and you have to time your jump onto it. Well in this article that is what we are going to be creating a moving platform! We are also going to be able to make sure are player is able to jump to the platform and that our camera stays with the player as we move farther into the level.
The first thing to do is let’s get the camera moving with the player. This is really easy. All you have to do it make the Main Camera and make it a child of the Player so it will move with it.
Now it is time to get the platform moving and grooving! To get started we need to set which point do we want the platform to be able to move to. In this situation we will be updating the x-position of the platform. So to do this I am going to set the end points by duplicating out platform and setting point A (13 on the x) at the beginning point and point B (25 on the x) at the end point on the x-axis. Next from both A&B remove the mesh renderer, filter, and collider. Create a C# Moving Platform script and attach it to the moving platform.
Once inside the moving platform script we need to know a few things first what our the positions of our to points we first must gather the transforms. Second how fast is our platform going to be moving, and third what is going to be the trigger to tell our platform when to switch back. Also we have to look up documentation on how we get our platform to move towards a specific location. Look at the example to see how this takes place.
The if statements are saying when the platform reaches its target destination switch back to the other destination.
Now that we have completed our moving platform we have to make sure when our player jumps on the platform he either does not fall off or through it once he lands on it. One of the first thing I had to do was add another box collider to the Moving Platform and raise the collider above the platform.
On the movingplatform script I added an onTriggerEnter method that is called when the player lands on the platform it parents the player to the platform. There is also an OnTriggerExit that removes the player from the platform’s hierarchy when it jumps off.
Fixed update is used when you are required to have physics movements instead of regular update. Instead of running at 60 fps it will run at 0.2 seconds it will get called. We also want to prevent the platform from changing rotation when the player lands on the platform. To do this go to the rigidbody component on the platform and make sure the constraints for freeze rotation are all checked.
Now as one progresses through their game they are going to want to have multiple moving platforms whether that’s to cross a moving bridge, a hole in the ground, or a band of enemies. So how do you have a bunch of moving platforms and it not clutter up your hierarchy you ask? Well to do this let’s make it modular.
First it’s best to separate the regular platforms from the moving ones. So I’m going to create a materials folder and create a material for the moving platform. In this case I’m going to make it green. Next I’m going to create an empty game object to house my moving platform and the moving points. Next set all of the transforms to 0,0,0. The level designer should be the one to decide how far point A,B are located. Finally prefab your moving platform.
Now new prefabbed platforms can be added into the scene. All that needs to be done is have their B location set.