Journal 77 — Unity 2.5D Platformer, Platforms Lives and Respawn

Objective: Finish rest of 2.5D Platformer basic setup

In the previous article, we set up the basic level with platforms, collectables, and jump effect for the player. In this article, we will finish the basic setup with moving platforms, lives and respawn system.

Camera Follow Player

To start, we need to fix the Main Camera so it will follow the player. We can simply make the camera a child of the player and it will always focus on the player.

Moving Platform

Let’s add a moving platform. We can create take one of the platforms and duplicate it to create a couple move points. Then we can remove the collider, mesh renderer and mesh filter for the two move points as that is not needed.

Next we will add a C# script to control the movement. Let’s use Vector3.MoveTowards. Let’s also use a bool variable for two conditions: if false, the platform needs to move to Point_B. If true, the platform needs to move back to Point_A.

Player Stick to Platform

When the player makes it to the moving platform, it does not “stick” to the platform.

We need to add functionality to check for a collision on the platform, then the player can be changed to a child of the platform. Then when jumping off the platform, the player is removed as a child and can freely move again. Let’s first get the OnTriggerEnter working to land correctly on the platform. Please note the change from Update to FixedUpdate, and also the platform RigidBody should also set constraints for x, y, and z rotation, so that no rotation affects the player if the player hits a corner of the platform.

However, when jumping off, we are still a child of the platform, so our movement still wants to go with the platform.

We can fix this by using OnTriggerExit and setting the player’s parent to null.

Moving Platform Modules

Let’s clean up the moving platform by making an empty game object that contains the moving platform object and Point_A and Point_B, then make it a prefab and give it its own color to distinguish this platform from others.

In our new grouped platform object, we want to 0 out the platform position and Point_A and Point_B positions so that the developer can duplicate and set these positions custom for each platform. Then we can create a new prefab of this group.

Finally, you can see the grouped platform in action. I can duplicate one and pick a new location and Points A and B.

Lives and Respawn

Let’s create a lives system. We can have a certain number of lives, and if we fall off the map, we can respawn and lose a life.

Let’s first get the display working. We can add a new text variable for lives in the UI Manager, add an integer in the Player script, and pass the number of lives to update the UI display.

Next, let’s create an empty game object at the player start and call it respawn point, and we can create a trigger with a RigidBody below the map that runs the length of the play area, and set it as a trigger.

Now, if the player drops off the map and collides with this trigger, we can add control to subtract a life from the UI display in the Player script. Let’s add a script on the Dead Zone object to check OnTriggerEnter if the Player collides, and send the player back to the respawn point. We can also include a reference to the respawn point.

The lives got subtracted correctly, but the respawn didn’t work. It looks like the velocity is falling so quick the respawn didn’t work. We can check this by disabling the Character Controller.

Now we know that is the issue, we can re-enable the Character Controller with a coroutine.

Thank you for your time!

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.