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

Philip Johnson
3 min readJul 4, 2021

--

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.

this bool is used to tell us if the player can perform a double jump or not. Without this boolean the player would be able to jump infinitely into outer space
The update loop in our player script will use this logic to allow the player to perform a second jump in the air.

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.

We follow a target’s transform.position with Vector3.Lerp and Time.deltaTime. It’s important to take note of the FixedUpdate loop as it was designed for unity physics and helps to avoid jittering with the camera.

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.

This logic will tell the gameobject it’s attached to that it needs to travel to point B if its current position is equal to point A and vice versa. This can work for both vertical and horizontal platforms

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.

Empty transforms will tell the platforms where to oscillate. ALso, notice that the platform has two collders. One to stand on and one to detect the player when landed on. This will help us keep the player moving with the platform

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Philip Johnson
Philip Johnson

No responses yet