FPS_Tutorial_2.pdf

(2427 KB) Pobierz
FPSTutorialPart2
Creating
a
First
Person
Shooter
(FPS)
Part
2
Author:
Graham
McAllister
Revised
by:
Jeff
Aydelotte
&
Amir
Ebrahimi
Time
to
complete:
3­4
hours
Last
Revision:
10­July­2009
197803557.009.png 197803557.010.png
Contents
1. Part 2: Enhancements
Prerequisites

3
Before
we
begin
­
level
setup

3
Weapon
switching

4
Rocket
Launcher

5
Hit
Points

16
Sentry
Gun

20
Skybox

22
Acknowledgments

23
197803557.011.png
Part
2:
Enhancements
This
intermediate­level

tutorial
extends
upon
the

Basic
FPS
tutorial
by

introducing
game
elements

such
as
multiple
weapons,

damage
and
enemies.
Prerequisites
This
tutorial
assumes
that
you
are
familiar
with
the
Unity
interface
and
basic
scripting

concepts.
Additionally,
you
should
already
be
familiar
with
the
concepts
discussed
in

Part
1
of
the
FPS
tutorial
series.
Before
we
begin
­
level
setup
Download
FPS_Tutorial.zip,
unzip,
and
open
the
project
folder
in
Unity.
If
you

have
completed
Part
1,
then
unzip
the
files
to
a
new
folder.
Import
the
Standard
Assets
Unity
Package.
Add
the
 mainLevelMesh
 and
FPS
controller
prefab
to
the
scene.
NOTE 
 In
this
tutorial
no
new
scripts
need
to
created.
We’ll
be
using
the
ones
that

were
downloaded
from
the
Unity
Package.
197803557.012.png 197803557.001.png 197803557.002.png 197803557.003.png
 
Weapon
switching
Before
we
discuss
how
to
create
each
individual
weapon,
we
need
to
write
some
code

to
manage
how
the
weapons
are
initialized
and
switched
from
one
to
another. 
Let’s

look
at
the
Javascript
for
 PlayerWeapons.js :
function Awake()
{
// Select the first weapon
SelectWeapon(0);
}
This
function
initializes
weapon
0
as
the
default
weapon.
function Update()
{
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
if (Input.GetKeyDown("1"))
{
SelectWeapon(0);
}
else if (Input.GetKeyDown("2"))
{
SelectWeapon(1);
4
197803557.004.png
}
}
This
function
detects
keyboard
input;
the
fire
button,
the
“1”
button
for
weapon
1
or

the
“2”
button
for
weapon
2. 
The
weapons
will
be
children
objects
of
the
Main
Cam­
era.
function SelectWeapon(index : int)
{
for (var i=0;i<transform.childCount;i++)
{
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
 
This
activates
the
corresponding
weapon
depending
on
keyboard
input.
Let’s
use
the
above
code.
Create
an
empty
game
object
called
Weapons. 
Move
this
so
that
it
is
a
child
ob­
ject
to
Main
Camera
(inside
FPS
controller). 
Our
weapons
will
be
added
as
chil­
dren
of
this
object.
Assign
the
 PlayerWeapons.js 
script
to
the
Weapons
game
object
under
Main

Camera.
We’ll
now
create
our
first
weapon.
Rocket
Launcher
This
section
will
describe
how
to
make
a
rocket
launcher
style
weapon. 
Launcher
The
rocket
launcher
is
responsible
for
instantiating
a
rocket
and
giving
it
an
initial
ve­
locity.
The
rocket
will
be
launched
directly
at
wherever
the
user
is
pointing
and
will
be

destroyed
whenever
it
collides
with
another
collider.
Add
an
empty
game
object
and
name
it
RocketLauncher. 
Position
the
game
ob­
ject
in
the
approximate
position
where
the
FPS
Controller’s
hands
would
be.
5
197803557.005.png 197803557.006.png 197803557.007.png 197803557.008.png
Zgłoś jeśli naruszono regulamin