10
Creating a First Person Shooter (FPS) Part 3 Author: Graham McAllister Revised by: Jeff Aydelotte & Amir Ebrahimi Time to complete: 3-4 hours Last Revision: 10-July-2009

FPS Tutorial 3

  • Upload
    sdddds

  • View
    88

  • Download
    5

Embed Size (px)

Citation preview

Page 1: FPS Tutorial 3

CreatingaFirstPersonShooter(FPS)Part3

Author:GrahamMcAllisterRevisedby:JeffAydelotte&

AmirEbrahimi

Timetocomplete:3­4hours

LastRevision:10­July­2009

Page 2: FPS Tutorial 3

Contents

1. Part3:AdvancedFPSPrerequisites 3Settingup 3Waypoints 3RobotAI 4RobotDamage 5Ragdolls 6Sound 7GUI 8Finally... 9Theendofthebeginning... 10

Page 3: FPS Tutorial 3

Part3:AdvancedFPS

PrerequisitesThistutorialassumesthatyouarefamiliarwiththeUnityinterfaceandbasicscriptingconcepts.Additionally,youshouldalreadybefamiliarwiththeconceptsdiscussedinPart1and2oftheFPStutorialseries.

SettingupWe’regoingtobuilduponthepreviousFPStutorial,sowe’llbeginbyopeningthat:

Openthepreviousproject(FPSTutorial2).

WaypointsThissectionwillintroducewaypointstoourgame,theseareusedtoinformtherobots ofthepaththattheycanwalkaround. Let’saddthreewaypointstoourgame:

CreateanemptygameobjectandrenamethistoWaypoint. Makesurethegameobjectisplacedapproximatelyonemeterabovethegroundlevel.

AddAutoWayPointscripttoWaypoint. NoticehowtheemptygameobjectnowdisplaysaWintheSceneView.

DuplicatetheWayPointgameobjecttwiceandarrangethewaypointsinatri­angleshape(positionquitefarapart).

Thisadvanced­leveltutorialextendsuponthepreviousFPStutorialsbyintroducinggameelementssuchaswaypoints,enemyAI,ragdollsandanimation.

Page 4: FPS Tutorial 3

Checkthatthewaypointsarevisibletoeachotherbyfirstlyclickingonanyway­pointthenselectingUpdateWaypointfromthecontextmenuoftheAutoWay­Pointscript(theright­mostbuttononthecomponentintheInspectorView). Agreenlinewillbedrawnbetweenvisiblewaypoints,aredlineforthosewhichdon’thaveaclearlineofsight.

Wehavenowdescribedthepaththatanenemycanwalkaround,nowlet’saddanopponenttothescene.

RobotAIThissectionwilladdanenemyrobottoourscene.

SelectRobotArtwork/robotanddragitintotheSceneViewmakingsurealloftherobotisabovetheground.

Let’sgivetherobotsomebehavior.

AddWeaponsScripts/AItorobot. IntheAIscriptsectionorrobot,assigntheFPScontrollerasthetarget(sotherobotknowswhotohuntdown).

AddtheAIAnimationscripttotherobot. Thiscontrolstheanimationofourro­bot(whentorun,whentoaimetc). ThiscommunicateswiththeAIscripttofindoutwhattheAIscriptiscurrentlydoing,e.g.arewerunning,shootingetc. Itthencrossfadestheanimationstoprovideasmoothtransition.

Nowweneedtomaketherobotcontrollerobjectalittlelargersothathedoesn’tin­tersectwiththeground. Thisisbecausethecharactercontrollerwhichisusedtopre­venttheenemyfrommovingthroughwallsisusingacapsuletorepresenttheAI. Weneedtomakethiscapsulealittlelargertomatchtheactualgraphics,thiswayhewillnotintersectwiththegroundanymore.

Selecttherobot,thenintheInspectorViewmodifytheheightandradiusvaluesoftheCharacterControllercomponentsothatitenclosestherobot. Pressplaytomakesureitworkscorrectly. 

4

Page 5: FPS Tutorial 3

Trymakingtheheightandradiusvaluessmallerandlargertoseethedifference.

Nowweneedtogivetherobottheabilitytofirehisgun. Therobotiscontinuouslymovingandanimating. Whenheshootshisgunweplayananimationonthehandsandthegraphicalgunthenspawntherocketfromtransformrelativetotherobot.Thistransformdoesnotanimate,itissimplyplacedapproximatelyatthepointwherethegunwillbewhentheshootanimationfirestherocket.

Createanemptygameobjectandmakeitachildoftherobot(usethehierarchyview).

Renamethegameobjectgun_spawn.

Nowweneedtoplaceitproperly. InthetransforminspectorselectResetfromthecontextmenu,thenmoveitforwardinthez­axis.

Addtherocketlauncherscripttothegun_spawngameobject. Assigntherocketprefabtotheprojectilevariable.

Makeaprefabofthisrobot,callitRobot. Thiswillallowyoutocreatemoreenemieseasily.

Playthegameandcheckthattherobotfiresatyou.

Althoughyoucanshoottherobot,itisnotconfiguredtotakedamage,we’llremedythisinthenextsection.

RobotDamageAttachtheCharacterDamagescripttotheRobotprefab.

Playthegame,shoottherobotwiththemachinegunandheshoulddisappear.

5

Page 6: FPS Tutorial 3

RagdollsRagdollsemulateanaturalskeletalbonestructure,thiswillallowourrobottofallnaturallywhenkilled. Thissectionwillshowhowtosetuparagdoll.

Firstly,createanewscene(File­>NewScene).We’regoingtosetupourragdollinhere. Saveyourcurrentsceneifnecessary.

Createacube(resizeifnecessary),thiswillbeusedasaplatformforourrobot.DragintherobottotheSceneViewsothatitispositionedabovethecube.

Removetheanimationcomponent. Thisisimportantotherwisetheanimationswillinterferewiththephysics.

Nowusetheragdollwizard:Gameobject­>CreateOther­>Ragdoll.

Adialogboxappears,nowwehavetoassignallthebonestothecorrectslotsinthewizard. WeassignthebonesbydraggingthevaluesfromtheRobotintheHierarchyViewontothecorrectplaceinthedialogbox.ExpandtheRobotgameobjectintheHierarchyViewifnecessarytorevealthis.

Firstly,rootHandle(HierarchyView)mapsontoroot(inthedialogbox). DragrootHandleontotheplaceholderbesiderootinthedialogbox.

SetTotalMassto4

LegsAssignupleg_LtoLeftHip. 

Assignlowleg_LgoestoLeftKnee.

Assignheel_LgoestoLeftFoot.

Repeatfortherightleg.

Upperbody/ArmsAssignupArm_LgoestoLeftArm.

Assignelbow_LgoestoLeftElbow.

Repeatfortherightarm.

Assignspine3goestoMiddlespine.

Finally,headgoestoHead.

Hitcreatethenpressplay,therobotshouldfall,howeverthegunwon’t.

Tomakethegunfall:

SelectthegunintheHierarchyView.

Addarigidbodycomponent.

6

Page 7: FPS Tutorial 3

Addaboxcollider. Adjustthesizeoftheboxcollidertofitthegun,you’llneedtoadjustthecenterpositionalso.

Nowweputthefullyriggedrobotintoaprefab.Tocreatetheprefab:

Assets­>Create­>Prefab.

RenameittoRobot­Ragdoll.

Dragtherobot(rootlevel)fromthehierarchyviewintotheprefab. Thiswillmakesurethegunisattachedalso.

Nowwhentherobotiskilled,wedeletetheoldrobotandinstantiatethenewragdollrobot,thisallowsourrobottofalltothegroundnaturally.

UsingtheRagdollOpentheoriginalscene(noneedtosavethecurrentscene),selecttherobotfromtheHierarchyViewandintheCharacterDamageinspector,dragintheRobot­RagdollprefabtotheDeadReplacement.

Playthegame,shoottherobotanditshouldnowfalldownwhenshot.

Youmaywanttotweakthemassoftherobottoimprovethelook&feeloftherag­doll.Youcandothisintheragdollwizard,bychangingthemassproperty. Thisises­peciallyimportantinexplosionforceswheretherobotisthrownintotheair.

SoundThissectionwilladdsoundeffectstoourgame.

MachinegunDragmachineGunSingleShotLoopableontotheMachineGungameobject.

TurnoffPlayonAwake.

7

Page 8: FPS Tutorial 3

RocketLauncherWewanttoattachthesoundsourceoftherockettotherocketsothattheaudiolev­elsreducesasitfliesaway.

SelecttheRocketprefab.

AddanAudioSourcecomponenttotheprefab.

DragtheRocketLauncherFireaudiosourcetotheAudioClippropertyoftheAudioSource.

AdjusttheRolloffFactorsothatthesoundfadesoutfaster(0.5mightbeok).

GUITheGUI(GraphicalUserInterface)isresponsibleforgivingfeedbacktotheplayeronthecurrentgamestatus. TypicalGUIelementsinclude;numberofbulletsleft,health,missionobjectivesetc.

InthisgamewehavethreemainGUIelements.

OverlayTheoverlaysetsupthedifferentzonesoftheGUI(health,ammoetc).

SelecttheGUIOverlaytextureinGUI/GUIOverlay.

SelectGameObject­>CreateOther­>GUITexture,thiswillplacethetextureinthecentreoftheGameView.

Wenowneedtopositiontheoverlayinthecorrectplace:

ModifythetransformPositionto0,0,0.

ModifythePixelInsetto0,0,256,128(inorderX,Y,Width,Height).

HealthSelectGUI/healthBarintheProjectView.

SelectGameObject­>CreateOther­>GUITexture.

SetthetransformPositionto0,0,0.

Thepixelinsetvaluesare37,83,111,29.

ThehealthBartextureisscaledinreal­timetocoverthecorrectamountoftherippledareabesidethehealthicon.

MachineGunSelectGameObject­>CreateOther­>Text.

SettransformPositionto0,0,0.

8

Page 9: FPS Tutorial 3

SetPixelOffsetto188,109.

Youcanchangethetextifwanted(fornow),ascriptwillsettextwhenthegameisrun.

RocketsSelectGameObject­>CreateOther­>GUITexture.

SetTexturetoNone.

DragMiscScripts/DrawRockets.jsontotheGUITexture.

SetRocketTexturetoGUIRocket21.

SetPosYto68.

Finally,addtheFPSPlayerscripttotheFPScontroller,assigntheGUIstothepropertiesintheFPSPlayerscriptsection.

Finally...We’venearlyfinishedourtutorial,butlet’saddsomeadditionalsoundsfirst.

PlayerDamageSoundsTotheFPSPlayerscriptoftheFPScontrollergameobjectassignthefollowingsounds:

moanOfPainSmall

moanOfPainBig

playerDie

WalksoundsWecanassignanynumberofwalksoundstoourFPScontroller(toavoidrepetition),thesearechosenrandomlyfromavariable­sizedlist.

SelectFPSplayer.ExpandthetriangleforWalkSoundsandsetSizeto5. Dragfootstep1­5fromSounds/intoeachofthefiveelements.

AddanAudioSourcecomponenttotheFPSplayer(noneedtodoanythingelse).

LightsformovingobjectsThesceneinourgameislightmapped,howeverthecharactersandrigidbodiescannotusethislightmapsincetheyaremoving.Thus,weneedtosetupsomelightswhich

9

Page 10: FPS Tutorial 3

onlyaffectthoseobjectsandlooksimilartothelightingofthelightmap. Forthispurposeweusethelightscullingmask.

Createadirectionallight.

We’llalterthelight’scullingmask,thisdetermineswhatthedirectionallightislight­ing.

FirstgotoEdit­>ProjectSettings­>Tags,clickonUserLayer8andtypeLight­mapped.

NowselectthedirectionallightagainandturnoffLightmapped.

SelectmainLevelMesh,changetheLayerpropertytoLightmapped.Applythischangetoallchildobjectswhenprompted.

Theendofthebeginning...ThiscompletesourFPStutorialseries. Manykeylessonsforgamedevelopmenthavebeenpresentedandwillapplytootherstylesofgames:notonlyFPS­stylegames. Wehopethatyouhavelearnedthefundamentalsofgamecreationandarenowmoti­vatedtodesignyourownnovelgameswithUnity.

AcknowledgmentsSpecialthanksgotoJoachimAnte(code)andEthanVosburgh(graphics)fortheirhelpinthemakingofthistutorial.

DownloadthecompleteFPSprojectfromtheResourcessectiontoseeapol­ishedversionofthistutorial.

10