0% found this document useful (0 votes)
1K views

Fps Tutorial

The document provides instructions for creating a first person shooter game tutorial in Unity. It describes setting up folders, prefabs, materials, scripts and sounds. It includes instructions on instantiating prefabs, destroying objects, adding rigidbodies and audio sources, and scripting arrays and parenting objects. Key steps include setting up the scene with a player, projectiles, targets and floor, adding materials and physics properties, and scripting shooting and spawning/destroying objects. The tutorial is intended for intermediate Unity users to learn game development techniques.

Uploaded by

api-297718659
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Fps Tutorial

The document provides instructions for creating a first person shooter game tutorial in Unity. It describes setting up folders, prefabs, materials, scripts and sounds. It includes instructions on instantiating prefabs, destroying objects, adding rigidbodies and audio sources, and scripting arrays and parenting objects. Key steps include setting up the scene with a player, projectiles, targets and floor, adding materials and physics properties, and scripting shooting and spawning/destroying objects. The tutorial is intended for intermediate Unity users to learn game development techniques.

Uploaded by

api-297718659
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

FPS Tutorial
Lesson Highlights
FPS Controllers
Instantiating Prefabs
Audio Sources
Audio Clips
Rigidbody Properties
Materials
Physics Materials
Scripting Arrays
Parenting Objects
Destroying Objects

Required Software
Unity (There is a free version of Unity available
at: http://unity3d.com/get-unity/download?
ref=personal)
Code editor (MonoDevelop, Visual Studio,
Notepad++, etc.)

In this exercise we will create a First Person Shooter game, complete with Physic materials, a scripted Array of sounds
that will play at random, and projectiles that are instantiated and destroyed via scripting. This tutorial will assume you
have a basic understanding of the Unity Game Engine, and your code editor of choice. Visit the Hello, World!, Unity
Interface, and Basic Script Elements tutorials for beginner tutorials. For this tutorial, I will be using a modified Unity
Layout, and Visual Studio Code.

Set Up
Start off by creating a new project, called FPS

!1

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Add in the Asset package called Characters. It will contain multiple character types, including the FPS character we
will be using.

Upon launching the Unity Editor, you will see that you now have two folders located inside of the Assets Folder:
Editor, and Standard Assets

We need to create five additional folders. Project > Create > Folder

!2

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

The five folders should be titled: _Scenes, Materials, Prefabs, Scripts, and Sounds. By naming the _Scenes folder
with an underscore as the first character, it forces the folder to be sorted to the top when sorted alphabetically.
While not essential, this is a best practice.

Now, in the Hierarchy, create three 3D Objects: Cube, Sphere, and Plane. The Sphere will act as our projectile, the
Cube will be our target, and the Plane will be our ground.

!3

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Set the following Transform properties for the Plane.

Set the following Transform properties for the Sphere.

Set the following Transform properties for the Cube.

Now, we have a good view of the Plane, Sphere, and Cube in the Scene panel.

!4

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Next, locate the FPSController prefab. It is located in Assets > Standard Assets > Characters >
FirstPersonCharacter > Prefabs. Click and drag it into the Hierarchy to add it to the current scene.

If we press Play right now, we will get the following warning.

This is because the Main Camera has an audio listener, and the FPSController has a camera attached to it, which
also has an audio listener. We only need one audio listener, and for that fact, we only need one camera. Lets delete
the Main Camera.

!5

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Now, change Cube, Sphere and Plane to Box, Ball, and Ground. These are much more meaningful names.

Set the Scale of the Ground object to 10, 1, 10

!6

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Add a Rigidbody component to the Box game object. Set the Mass to 0.5 so that the box is lighter and flies around
nicely.

Add a New Script to the Box called BoxScript. We will leave it empty for now.

Add a Rigidbody to the Ball, and set the Mass to 2, so that it has more weight which will make for a more forceful
impact when it hits the boxes.

Add a New Script to the Ball, called BallScript. We will also leave it empty for now.

!7

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Ok- time to make sure were staying organized. Place both the BallScript and CubeScript into the Scripts Folder. We
dont want to let ourselves get disorganized.

Weve done enough to need to save our scene. Save as Scene01 in the _Scenes Folder.

!8

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Create three new materials in the Materials Folder. Change the color of each by selecting the Albedo element. You
should have one Ball Material, one Box Material, and one Ground Material, each with a different Albedo value /
different color.

Now we have three different colors on our three different objects, which makes it much easier to differentiate
between the objects, but also makes it far more visually appealing.

!9

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Now, in the same Materials Folder, create two New Physic Materials called Ball Bounce, and Box Bounce. 1 is ON and 0
is off, so we will turn on Bounciness, by making sure to set both to a Bounciness of 1.

Set the Ball Bounce material as the Material on the Balls Sphere Collider, and set the Box Bounce material as the
Material on the Boxs Box Collider

!10

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Scripting

Scripting the Ball


Edit the BallScript so that it matches the following:

Here is a commented explanation of what all the code does for us.

!11

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Scripting the Camera


Add a New Script called Shoot.

Write the following code, and save.

If you return to the Unity Editor, you will find the following warning:

In order to fix this issue, we need to change the name of the GameObject called camera, because camera is already
taken. Lets call it playerCam instead.

!12

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Remember to also update the camera.transform.rotation to playerCam.transform.rotation

Here is a commented explanation of what all the code does for us.

Now, drag the Ball and Box into the Prefabs Folder, which will turn them into prefabs.

!13

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Delete the Ball and Box from the Hierarchy. This will leave you with only the Directional Light, Ground plane and
FPSController game objects

Without proper adjustment, the ball will be instantiated from the top middle of the screen. In this tutorial, we are
trying to make it look like the ball is thrown from one arm, not launchd form the players head, so this simply wont do.
A simple way to fix this is to create an object that is offset from the camera/player that instantiates the ball.
This is also a technique that can be used in a traditional FPS game. In a game with a gun/launcher, you would simply
place an item that instantiates the ball/bullet/projectile at the end of the barrel of the gun/launcher.
To create an object that will act as the point of instantiation, we will use an Empty game object. We want this game
object to be located relative to the player/camera, so we will make it a child of the FPSController game object.

!14

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Name the new Empty game object Player, and attach the Shoot script.

Set the following public variables: Ball Prefab: Ball, Player: Player, Player Cam: Player

In order to ensure that the ball always travels forward, and doesnt spin (which will make it fly in all sorts of crazy
directions) we need to make sure the Rigidbody Constraints are set as Frozen Rotation on X, Y, and Z.

!15

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

We need to place boxes on the plane so that we have something to use as a target, like shown in this example. We will
be placing ten boxes.

I have personal preference for how to handle multiple instances of an object that I will share with you. Start off by
placing eleven boxes on the plane, randomly distributed along the X and Z axes.

WAIT! Didn't we just agree that we only needed ten? Correct, but again Im showing you my preferred method for
handling multiples. Unity does a really nice job of numbering the additional instances. However, the first isnt
numbered, meaning once you have ten created, you will actually have numbers 1-9, and one un-numbered. Thats a
bit messy for my taste, so I like to go ahead and create eleven, then simply delete the first, un-numbered instance.

!16

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Numbered 1-10 Boxes, shown in the Hierarchy.

Now, Im going to create an Empty called Box Container, place the ten boxes in it, and duplicate Box Container, six
times, deleting the original, so I end up with five nicely numbered box containers, for a total of fifty boxes.

!17

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Assign various Transform properties to each Box Container, so that you get a nice even distribution throughout the
level.

Your final box layout should look something like this:

If you find that the Ball sometimes flies directly through the boxes without knocking them over, down, and around,
you are probably experiencing the ball move through the box collider faster than the system is updating to detect the
collision. I have found that setting Thrust to 700 is a fairly good speed for the ball.

You can also change the projects Timestep. This effects how fast or slow the game is processing information. Going
too crazy with this, especially on a graphically intensive game can be disastrous, so use caution, however for this
project, we wont do anything that hurts playability.

!18

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Go to Edit > Project Settings > Time

Set the Fixed Timestep to 0.01 - This should prevent any missed collisions.

!19

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Sound
The game works, however in order to make it a bit more realistic, we need to add sound effects to the boxes so that
they seem a bit more like real objects. Attached to this tutorial is a folder with six sounds. We are using six different
sounds so that the sound effects are more realistic and dont just sound like the same single sound over and over
again.
Start off by adding an Audio Source to the Box game object.

Make sure Play On Awake is NOT checked. If this is left checked, the sound will play as soon as the Play button is
pressed, and we only want it to play when we tell it to (which will be on collision)

!20

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Locate the six box sound files located in the .zip folder. Place these six sounds in the Sounds folder.

Open up the BoxScript.cs file and write the following:

Here is a commented explanation of what all the code does for us.

!21

Unity Game Engine - FPS Tutorial - Intermediate

CCE VGD/GP

Now, back in the Unity Editor, you will notice the Audio Clip array showing up under the BoxScript. Change the Size to
6 and attach the box sounds to each of the following Element slots as shown.

Thats it! The game is working!


For a further challenge try the following:
Add an array of sound effects for the ball
Make the blocks disappear when they are out of view
Implement a point system with a win screen

Learning C# for Unity - Hello World by Levi Sterling is licensed under a Creative Commons Attribution 4.0 International
License. This tutorial was written on Mac OSX, using Unity (Educational License), Visual Studio Code, and Pages.

!22

You might also like