3D Survival Shooter

Creating a new project with Unity Character Controller

Simon Truong
5 min readMar 21, 2022

--

FPS games (aka First person shooters) are extremely popular within the gaming industry. In this article we will start by creating a new project gear towards FPS games.

Creating a new 3D Unity File:

Just a refresher for those that are starting out new to Unity, to create a new file we are going to open up the Unity Hub and select “New Project”.

Then we are going to select the 3D core as our project template, while selecting the save location and giving the project a name.

Once Unity is finished compiling all the data, you will be greeted by the Unity UI. Before we can drive in, we are going to readjust the UI windows to provide maximum efficiency. You want the Inspector and Project/Hierarchy towards the right side, while the Scene view is on the top left and the game view is on the bottom left, this way you are able to see real time adjustments that you make within the game.

Working with Unity Character Controller:

Unity offers a lot of features, one of them is the character controller, this feature is great because it allows you to create player base game objects without using rigidbody like we did with the mobile 2D game. It also comes with all the nice settings such as Slope Limit and collision detection.

To start using the character controller, we first need to create our floor and player game object. For the Floor, I just used a 3D cube which was stretch by 20 units on the X and 20 Units by the Z. Then I created an capsule game object which I named “Player”.

Within the Player object, we are going to add an “Character Controller” component. This will automatically apply the necessary functionality to start scripting our player controller unit.

The next thing we are going to add is a brand new C# script, which we will dub “Player”. Inside this script we are going to tell the controller to move and jump whenever we press certain inputs from our keyboard.

By default, we don’t need to adjust anything for the Character Controller.

The player script:

In the player script we are going to focus on severeal aspects, first will be player movement using the input system, next we are going to create a jump function.

To begin we first need several global variables for speed, gravity, jump height, access to the character controller component, and a global vector3 called direction.

I made some of my variables Serialized so I can adjust them in the Inspector

In the start method we are going to get access to the character controller, which is simple because the script and character controller are on the same game object.

Next we are going to create a new method called “Movement”. To begin the movement, we first need to make sure that the player is grounded, otherwise the player that is mid-air should not do any movement unless it is jumping.

Start with an IF statement to check if the player is indeed grounded, then we are going to assign some local float variables called “horizontal” and “vertical”. These two variables will be the Input system.

Next we are going to assign a new vector3 value to the direction variable. By default we set the direction to be (0,0,0) now we need to change them according to our input values. At the same time we are going to multiply the direction with our speed variable.

We can also create the jump function within this IF statement, since this IF statement is checking whenever the player is grounded is true, we will allow the player the ability to jump only when they are grounded.

Using the Input get Key down and KeyCode function, we can adjust the direction Y value to be our set jump height.

Now only if the player is grounded can they jump.

Finally to end the movement method, we are going add in gravity, since we don’t want the player to just float away, we are going to set the direction -= the gravity value multiplied by Time.deltatime.

Then we can finally set the actual movement to our character controller by using the function “controller.Move(direction * Time.deltatime)”.

Now gravity will always be applied when you jump or fall, and the movement is controlled by the direction and speed.

Back in Unity, you will see that the script now is updated with adjustable values within the inspector.

And your player should be able to jump and move and also collide with 3D objects within the scene.

--

--

Simon Truong
Simon Truong

Written by Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

Responses (1)