Day 14: Moving the camera with the player position and moving platforms

Objective: To create functionality that allows the camera to follow the Players x and y position and to implement the moving platform mechanic to mix up the gameplay.
Picking up where I left off from the last session, I added collectible coins that update the UI with the players current number of collected coins. This is the same technique used to collect the powerups in our shooting game so I won’t talk about how this is done. You can find that article here https://philipjohnson-88332.medium.com/day-13-creating-a-2-5d-platformer-game-with-unity-physics-967d0643b64e. I also implemented a double jump feature.


Now that the Player is moving and jumping along in our environment, I need to tell the Camera in the scene to follow the player and keep them in view. To do this I wrote a script for the main camera that will follow a target’s transform using the Vector3.Lerp method. This effect can be achieved with different move methods provided by MonoBehaviour, but for the purpose of this simple platformer playground this will work fine.

So now we have a moving camera in this boring little environment made up entirely out of platforms. To add a level of difficulty and gameplay design I decided to create moving platforms that our player can ride from point A to point B. Moving platforms are great because they force the player to rely on their timing to land correctly and avoid having to respawn from a fall.

In unity we will create empty transforms and call them Point A and Point B. These can be placed however we like within good reason.

You’ll notice the second collider sitting on top of the platform. That is a trigger collider that will tell us when a player has entered the platform. When it detects the player it will set the parent of the player to be the platform we’re currently standing on. Once we exit the trigger, the player’s parent will be set to null so that we can move freely. This is how we keep the player from sliding off of our platforms due to how we handle the player’s movement. This is all seen in the gif above at the beginning of this article.

In the next session I will wire up the health system for the player and we will create some clever puzzle mechanics that will breathe more life into this platformer to make it more fun. I will also replace the prototype assets in this project with real art assets and animations.