3D Platformer GameDev Series – Weekly Blog #2: Player Movement, Jumping, and Camera Tracking

Alrighty, it’s Sunday! As I mentioned last week, at the end of every week, I shall update everyone on my progress in the 3D Platformer Udemy course I am taking. If you have not checked out my opening blog post for this GameDev series, I do recommend having a read there as a preface before carrying on here.

So, am I enjoying this course so far? Absolutely! I must say, when it comes to understanding the course material, my decision to take a slow and measured approach to learning is really helpful, particularly in my retaining that knowledge. To prove how effective my approach has been, I will demonstrate today how basic player movement, jumping, and camera tracking can be achieved without referring to any of the course material.

To begin making a 3D platformer in Unity, the first step is to setup the game environment or scene. In Unity, the default 3D game scene contains a basic camera and a simple light source. The first thing to include in the scene is… well, a platform! For this, a simple planar surface is added to the scene – and just so it resembles the ground a bit more, a green material is applied to it. Next, a player model is required. Here, a 3D model of a robot is used (this was an asset provided in the course):

BasicGameEnvironment

After the game scene is set up, basic (planar) player movement and jumping can be achieved by the following C# script (Note: the coding in the yellow text boxes allow the player to “jump” while the rest of the code is responsible for lateral movement using the “WASD” keys, arrow keys, and/or game controller):

2. BasicPlayerMovement (with jumping)

I won’t get into what each variable, function, and parameter does in each line of coding (which in my opinion is all very interesting), but I can assure you that I can explain all of it as a result of this course. You see, the course instructor, James Doyle, has done an amazing job in explaining all of the concepts clearly and concisely – even using examples to illustrate his point at times. Essentially, what I can show is that the following result is produced when the above script is attached to the player model:

basicplayermovementandjump

So far so good. However, we run into an issue where the player can walk off of the screen and/or disappear out of the camera’s view. How do we get around this? What is crucial in any 3D platformer? Camera tracking. For this, Unity has in its Package Manager toolkit an asset called “Cinemachine“. After installation, a “FreeLook” Camera object is conveniently added to the game scene. Once added, it replaces the default main camera and a plethora of camera settings are then available:

UnityCinemachine

First (see blue box in the picture above), the “Follow” parameter is set to reference the player mesh and the “Look At” parameter to reference the mesh of the character’s head. After, the following script (see coding in red boxes below) is necessary for the camera tracking to work whenever the player inputs commands to move the character around the environment (Note: The third red box down replaces an earlier line of coding which now allows the player to move in the appropriate direction regardless of where the camera is positioned around the player):

4. BasicPlayerMovement (camera tracking)

Again, I won’t go into how the coding works, but I can show that the script above produces the following result:

cameratrackingplayermovement

Notice how the camera is now following the player (the robot) around the environment and when the mouse is moved around, the camera is essentially looking at the robot’s head. Indeed, I have found that Cinemachine is a powerful tool in Unity.

To conclude today’s blog post, whether you are a game developer reading this or not, I hope that you can appreciate the work and process behind making a 3D platformer. And along the way, I hope this will inspire you to start or continue on a project of your own. I look forward to next week, where I will demonstrate here how to implement player rotation and animate our player robot to give him some form of (apparent) life! Stay tuned!

– Taklon