Unity First Person Controller

In this section, you'll learn the mechanics of first person shooter games. We'll have a character that can walk around and shoot things, and we'll have an enemy that can shoot back at us. You'll also learn about the following:

  • Learn how to download models and assets from the Unity Asset store
  • How to add textures to a 3D object
  • How to add animations to your game
  • How to display health and player lives on screen
  • How to add pickups to a game like Health, and how to add a health bar
  • How to add Respawn points to your game

Again, we'll keep it basic with just a simple corridor. But this is a big section, so let's get started.We're going to have a something called a first person controller. This will be our player. We'll download a ready-made one from the Unity Asset store.

So, create a new 3D project in Unity. Call it anything you like. When the project loads, you'll have a default scene with the name Sample Scene. You can rename this, if you want. To do so, click on your Assets > Scenes folder in the Project area at the bottom of Unity. Select the Sample Scene and right click. From the menu, select Rename.

OK, let's add a plane so that our character has something to walk on. From the Game Object menu at the top of Unity, select 3D Object > Plane:

Unity menu showing the Plane item selected

Type Floor as the name for your Plane. With the floor selected, change the scale to 4 for the X, Y and Z values in the Inspector. And make sure all the other values are 0:

The Floor item in the Inspector showing the settings

You can add a bit of color to your floor by creating a Materials folder in the Project area of Unity, then create a new Material. (You did this in the previous section for your Drive game. If not, learn how to create a new material by clicking here: LINK.) Add a colour to your material (Albedo.) Here's what your Project area should look like:

A material for a floor

And here's what your Hierarchy and Scene view should look like: (We've gone for a grey floor.)

A 3D pane with a material

OK, now that we have a floor to walk on, let's add a character.

 

Downloading a First Person Controller

Rather than creating our own First Person Controller, we can download one from the Unity Asset store. So, from the menus the top of Unity, click on Window. From the Window menu, select Asset Store. This will launch your browser and log you in to the store. If not, fire up a browser and go to this page:

Unity Asset Store

In the search box at the top, type First Person Controller. From the results you get back, click on Mini First Person Controller. (It's by ViteFait.) Or, try this link:

First Person Controller Page

If the link still works, the page should look something like this:

Unity asset store showing the mini first person controller

You'll need to log in to download the asset. If you're downloading the asset for the first time, the blue button above will say Add to My Assets (or something similar). At the top of the screen, you'll see a banner asking you to either Open in Unity or Go to My Assets. Click the Open in Unity one. You'll then see the Package Manager appear:

Unit package manager

Click the button in the bottom right to download the asset (yours might say Download).

Select the asset on the right (you won't see all the items on the list above, just your own assets.) Then click the Import button bottom right. You'll then see yet another screen. This one:

Importing an asset from the package manager in Unity

Import the Mini Controller and a new folder will appear in the Project area:

The first person controller showing in the Project area of Unity

If you want to use the First person controller in another project, just go to Window > Package Manager from the menus at the top of Unity. You'll then be able to import it without going back to the Asset store. You might see no items displayed, however. In which case, click the dropdown at top left to see a menu. Select My Assets from the menu:

The package manager showing the My Assets menu item

If it's still showing blank, click the reload button bottom right, or the Load link.

 

Download the Gun

While you have the Asset Store open in your browser, you might as well download a gun model. In the search bar at the top of the Unity Store, type in Handgun. The one we'll go with is a free asset by Nokobot, a game developer. Click the link for that one. If you can't find it, the direct page is here, at the time of writing:

Handgun Asset

Go through the same process to add it to your assets. Download and import it into Unity and you should see a new folder labelled Nokobot:

The Modern Handgun item by Nokobot showing in the Project area of Unity

We'll add the gun shortly. For now, let's add the player.

Click back on the First person controller folder. Drag the First Person Controller (Full) item onto your scene: (The other one is a stripped down version.)

Dragging the first person controller onto the Scene

When you let go of your left mouse button, you should see a capsule in your scene.

A close-up in Scene view of the first person controller

If you have a look in the Hierarchy, you'll see this:

The Unity Hierarchy showing the first person controller item selected

By default, Unity gives you a Main Camera. We don't need this as the First Person Controller has a camera attached. Select your Main Camera and delete it. (Either right click and select Delete from the menu, or just press the Delete key on your keyboard.) Your Hierarchy will then look like this:

Deleting a Main Camera in the Hierarchy

If you expand your First Person Controller item, you'll see four other items grouped under it. We did the same thing in the previous section, when we created our car - grouped items under one parent item.

Test it out now. Play your game and press the WSAD keys on your keyboard. You should find that you can walk about. (You should also be able to hear the sound of footsteps as you move.)

If you want to change the speed at which your character moves, make sure the main parent item is selected for your control. In the Inspector on the right, notice the three scripts: Movement, Jump, and Crouch (don't worry if your Position values are not the same as ours.):

The properties of a first person controller showing in the Inspetor

The speed is set to 5, by default. You can change it to anything you like. Notice, too, that you can run by holding down the left shift key (and the W key). Crouching is done by holding down the left CTRL key. Again, you can change these to anything you like.

 

In the next lesson below, we'll build some corridors for the player to walk around in. You'll also learn about textures.

<--Back to the Unity 3D Course Contents Page