You are on page 1of 31

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations.

Page 1 of 31

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations

Ferdinand Joseph Fernande


!hief "e#hnologi#al $ffi#er% Dreamlords Digital &n#. Admin and !o'fo(nder% Unity Philippines Users Gro(p )eptem*er +,11

"his do#(ment e-#ept #ode snippets is li#ensed .ith !reati/e !ommons Attri*(tion'0on!ommer#ial 3., Unported

All #ode snippets are li#ensed (nder !!, 1p(*li# domain2

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page + of 31

Overview
Again% .e3ll *e #ontin(ing .here .e left off from the pre/io(s lesson. &f yo( ha/en3t done Lesson 4% yo( need to do it *efore yo( #an #ontin(e here. 5e3/e gotten fairly far in o(r simple game: .e #an #ontrol the player% the enemy #hases (s% and .e #an shoot enemies (ntil they stop. 0o. .e3ll start adding a *it of polish *y adding animated 3d models. &n a real prod(#tion en/ironment% yo(3d still *e (sing pla#eholder art for a long time (ntil the artists nail do.n the definite 3d model for the #hara#ters. )ometimes tho(gh% as the programmer% yo( #o(ld *e as6ed to p(t in an (n#olored% test 3d model into the game. )ometimes% people .ant to see a test of ho. the end prod(#t .o(ld end (p loo6ing li6e .hen played. "his means sooner or later% yo(3ll need to 6no. ho. to integrate a 3d model into yo(r #hara#ters. "hat3s .hat .e3ll *e learning in this lesson. F(rthermore% as yo(3ll see later% o(r #(rrent #ode a#t(ally has some *(gs that .e3ll only noti#e on#e .e add the 3d models7

A Note About 3d Models


3d models are shapes made *y a 3d artist. &t (s(ally also has animations. Animations are sets of mo/ements that happen on that 3d model. For e-ample% a 3d artist #o(ld ha/e made a om*ie 3d model% and made for it a .al6 animation% a standing animation% an atta#6 animation% and so on. As the programmer% .e3ll display the 3d model% and play the #orresponding animation on proper sit(ations. For e-ample% .hile the 8nemy game o*9e#t is mo/ing% the .al6 animation sho(ld *e played.

Importing The Zombie 3d Model


5e3ll *e (sing a ready'made asset .ith animations. First% ma6e a ne. folder inside :"heGame;. 0ame it :Models;. 5e3ll p(t all models there.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 3 of 31

<o(r lesson assets folder sho(ld #ome .ith a folder inside named =om*ie. !opy that .hole =om*ie folder inside the Models folder in Unity. &t .ill ta6e some time to import the assets. $pen yo(r =om*ie folder and inside sho(ld *e an asset named :=om*ieMi-amo;. "his is the 3d model of the om*ie. &f yo( sele#t it% it sho(ld sho. yo( a pre/ie. of the 3d model.

0o. drag =om*ieMi-amo into the s#ene. Most li6ely% yo( .on3t see anything. &ts a#t(ally there% *(t (nfort(nately too tiny to *e seen. )ele#t the =om*ieMi-amo in the Pro9e#t >ie. again. Loo6 at the &nspe#tor. <o(3ll see one of the #omponents is the F?@&mporter. Under that sho(ld *e a property #alled :)#ale Fa#tor;. Aight no. its set to ,.,1. "his is .hy its so small. !hange the /al(e to ,.B4. "hen s#roll do.n the &nspe#tor and at the *ottom sho(ld *e t.o *(ttons: :Ae/ert; and :Apply;. !li#6 on :Apply; so yo(r #hanges .ill *e applied. &n effe#t% the )#ale Fa#tor sho(ld *e in#reased so .e #an see the om*ie. &t sho(ld end (p a*o(t the same height as o(r *o- 8nemy.
Information Aemem*er% one Unity #(*e is 1 meter in height. $(r 8nemy #(*e is s#aled at 11%+%12 so its + meters in height. "he a/erage person sho(ld *e some.here aro(nd + meters.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page C of 31

Illustration 1: The Zombie 3d model should end up taking about the same height as our 2m-high Enemy box.

Using the Zombie 3d Model


0o.% .e are going to stop (sing the #(*e 3d model for o(r 8nemy and (se the =om*ie instead. )ele#t one of yo(r 8nemy game o*9e#ts. 5e3ll *e editing that one. After .e edit it% .e3ll 9(st apply the #hanges to the prefa*. "his ma6es it so that all enemies .ill end (p (sing the =om*ie 3d model. Aemem*er% .ith a prefa*% .e only need to ma6e #hanges on#e. "he other prefa* instan#es #an get those #hanges a(tomati#ally. First of all% #hange the 8nemy3s s#ale from 11%+%12 to 11%1%12. 5e3ll *e remo/ing the #(*e so it doesn3t ma6e sense to stret#h the game o*9e#t anymore. 5e3ll need to remo/e the !(*e. Find the :!(*e 1Mesh Filter2; #omponent and remo/e it. Go ahead and #li#6 :!ontin(e; on the .arning. Do the same for the :Mesh Aenderer; #omponent. "hat Mesh Aenderer .as *eing (sed *y the #(*e.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page B of 31

)et the 8nemy game o*9e#t3s < position to ,.B% so the pi/ot point ends (p on the gro(nd. &f yo(3re not seeing it that .ay% remem*er to set Unity to sho. pi/ots instead of #enters. 5e3ll p(t the om*ie inside the 8nemy no.. Parent the =om*ieMi-amo game o*9e#t into the 8nemy game o*9e#t.

0o. inside the 8nemy% sele#t the =om*ieMi-amo game o*9e#t. &t sho(ld *e a #hild no.. )et its position to 1,%,%,2. Doing this .ill #enter the =om*ie to the 8nemy game o*9e#t. 0o. .e need to fi- the !hara#ter !ontroller. "hat3s the green sphere 1.hi#h is a#t(ally a #aps(le2 that yo(3re seeing right no.. !hange its /al(es to these:
Deight: + Aadi(s: ,.C !enter: 1,%1%,2

"he !hara#ter !ontroller3s #aps(le sho(ld end (p en#aps(lating the om*ie. &ts fine if the arms protr(de o(t of the #aps(le. <o(3ll noti#e .e made it that the 8nemy3s pi/ot point ended (p at his feet. "he 8nemy3s position then% indi#ates his position starting from his feet% instead of from his torso. 5e do this so that the <'a-is /al(e properly sho.s his height from the gro(nd.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page E of 31

!li#6 on the prefa*3s Apply *(tton no. so all yo(r enemies .ill ha/e the =om*ie 3d model. &f yo( r(n the game no.% yo(3ll see a fe. *(gs. $ne% the animation pa(ses 1.hen it sho(ldn3t2. And most noti#ea*ly% t.o% the om*ies are floating in the air7

"his is simply *e#a(se .e ha/en3t added gra/ity to o(r 8nemyMo/ement s#ript. &n #ontrast% o(r PlayerMo/ement s#ript already has gra/ity% so its a simple matter of applying the same idea to the 8nemyMo/ement s#ript.
Information &t .o(ld a#t(ally *e *etter if .e made a *ase #lass that does the #ommon things *et.een PlayerMo/ement and 8nemyMo/ement. ?(t .e3ll .or6 on that later.

Adding Gravity To nemies


5e simply #opy some of the #ode from PlayerMo/ement to 8nemyMo/ement. $pen yo(r 8nemyMo/ement s#ript and add the #hanges *elo.:
01 02 03 04 0" 0$ 0& 0) 0* 10 using UnityEngine; using System.Collections; public class EnemyMovement : MonoBe aviou! # C a!acte!Cont!olle! %cont!olle!; '!ans(o!m %playe!; +Se!iali,e-iel./ (loat %moveSpee. 0 ".0(;

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page F of 31

11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 31 32 33 34 3" 3$ 3& 3) 3* 40 41 <

+Se!iali,e-iel./ (loat %g!avity 0 2.0(; (loat %y1elocity 0 0.0(; voi. Sta!t23 # 4ame5b6ect playe!4ame5b6ect 0 4ame5b6ect.-in.4ame5b6ect7it 'ag289laye!83; %playe! 0 playe!4ame5b6ect.t!ans(o!m; %cont!olle! 0 4etComponent:C a!acte!Cont!olle!;23; < voi. Up.ate23 # 1ecto!3 .i!ection 0 %playe!.position = t!ans(o!m.position; .i!ection.>o!mali,e23; 1ecto!3 velocity 0 .i!ection ? %moveSpee.; i( 2@%cont!olle!.is4!oun.e.3 # %y1elocity =0 %g!avity; < velocity.y 0 %y1elocity; %cont!olle!.Move2velocity ? 'ime..elta'ime3; <

)ame idea .ith the gra/ity in PlayerMo/ement% .e simply ha/e a < /elo#ity that gets to ha/e a negati/e /al(e .hen the #hara#ter is in the air. "his #a(ses the #hara#ter to fall. "est the game no.. "he om*ies sho(ld stay on the gro(nd as e-pe#ted.

Ma!ing The Zombies "a#e The $layer


<o(3ll noti#e the om*ies don3t *other fa#ing the player .hen #hasing him. "he fi- for that is simple. 5e3ll need to edit the 8nemyMo/ement s#ript for this:
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 using UnityEngine; using System.Collections; public class EnemyMovement : MonoBe aviou! # C a!acte!Cont!olle! %cont!olle!; '!ans(o!m %playe!; +Se!iali,e-iel./ (loat %moveSpee. 0 ".0(; +Se!iali,e-iel./

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 4 of 31

13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 31 32 33 34 3" 3$ 3& 3) 3* 40 41 42 43 44 4" <

(loat %g!avity 0 2.0(; (loat %y1elocity 0 0.0(; voi. Sta!t23 # 4ame5b6ect playe!4ame5b6ect 0 4ame5b6ect.-in.4ame5b6ect7it 'ag289laye!83; %playe! 0 playe!4ame5b6ect.t!ans(o!m; %cont!olle! 0 4etComponent:C a!acte!Cont!olle!;23; < voi. Up.ate23 # 1ecto!3 .i!ection 0 %playe!.position = t!ans(o!m.position; .i!ection.>o!mali,e23; 1ecto!3 velocity 0 .i!ection ? %moveSpee.; i( 2@%cont!olle!.is4!oun.e.3 # %y1elocity =0 %g!avity; < velocity.y 0 %y1elocity;

t!ans(o!m.!otation 0 Auate!nion.BooCDotation2.i!ection3;

%cont!olle!.Move2velocity ? 'ime..elta'ime3; <

8ssentially% .e need to #hange the om*ie3s #(rrent rotation to ma6e it fa#e the player. :transform.rotation; is .hat .e (se to #hange o(r #(rrent rotation. Aemem*er the dire#tion /aria*le that .e ha/e in line +F. &f yo( #an remem*er% that3s the dire#tion .e (se to mo/e to.ards the enemy. !oin#identally% its also the dire#tion needed to *e a*le to fa#e the player. G(aternion.Loo6Aotation is the f(n#tion that .e (se to ta6e that dire#tion and #on/ert it to the format that transform.rotation needs. "his format is the H(aternion. G(aternions are (sed to represent rotations% 9(st li6e 8(ler angles. 0o. test the game. Unfort(nately% it doesn3t H(ite do as e-pe#ted. 5hile the om*ies do fa#e the player% yo( may see them fli#6ering too fast. "hey3ll also rotate (p and do.n .hen yo( 9(mp.

Information 5hy are .e *othering .ith these :H(aternions;I &t t(rns o(t 8(ler angles ha/e some short#omings .hen (sed in an intera#ti/e 3d .orld li6e Unity. 8(ler angles s(ffer from :gim*al lo#6;. Dere3s a good <o("(*e /ideo that e-plains the pro*lem: http:JJ....yo(t(*e.#omJ.at#hI/KrrU!?$lJdtC. 0o.% H(aternions on the other hand% don3t s(ffer from gim*al lo#6% among other ad/antages. "his is .hy its (sed a lot in Unity. Unfort(nately% H(aternions are hard to (nderstand .hen yo( loo6 at its /al(es. "hey are #omposed of fo(r n(m*ers% designated -% y% % and .. ?(t they3re not e-pressed in , to 3E, degrees li6e 8(ler angles are. Dere3s an e-planation on ho. H(aternion /al(es are (sed as rotations: http:JJen..i6ipedia.orgJ.i6iJG(aternionsLandLspatia lLrotation. &f its too #onf(sing% don3t .orry. <o( don3t need to (nderstand everything a*o(t H(aternions. <o( 9(st need to 6no. eno(gh to (se them in Unity.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 9 of 31

Illustration 2: The ode doesn!t "uite do as expe ted.

"he om*ies rotate (p and do.n *e#a(se the dire#tion that they loo6 to.ards has a <'a-is /al(e. "he <'a-is represents the (p'to'do.n .hen .e tal6 a*o(t dire#tions. "he fi- is simple. 5e simply modify the dire#tion (sed so it has no <'a-is /al(e:
2" voi. Up.ate23 2$ # 2& 1ecto!3 .i!ection 0 %playe!.position = t!ans(o!m.position; 2) .i!ection.>o!mali,e23; 2* 30 1ecto!3 velocity 0 .i!ection ? %moveSpee.; 31 32 i( 2@%cont!olle!.is4!oun.e.3 33 # 34 %y1elocity =0 %g!avity; 3" < 3$ 3& velocity.y 0 %y1elocity; 3) 3* 40 .i!ection.y 0 0; 41 t!ans(o!m.!otation 0 Auate!nion.BooCDotation2.i!ection3; 42 43 44 %cont!olle!.Move2velocity ? 'ime..elta'ime3; 4" < 4$ <

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 1, of 31

"he om*ies sho(ld no. *e fa#ing yo( as e-pe#ted.

Using The Zombie %al! Animation


0o. .e #an #ontin(e on the fo#(s of this lesson. <o(3ll see the om*ies standing still and sliding off the floor .hen mo/ing to.ards yo(. 5e3ll *e fi-ing that. 5e need the om*ie to play its .al6 animation. )ele#t the :=om*ieMi-amo; game o*9e#t. "hen loo6 at the &nspe#tor. <o(3ll see it has an Animation #omponent. $ne of its properties is a list of animations. "hose are the animations that the om*ie 3d model has.

<o(3ll see a :Mo/e1;% :Mo/e+;% and :Mo/e3;. "hose are the three different .al6 animations that .e #an (se.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 11 of 31

5e #an a#t(ally 9(st ma6e the 3d model play the proper animation that .e .ant (sing the &nspe#tor% .itho(t any #oding. ?(t .e3ll do some #ode so .e #an #ontrol the animation *etter. Ma6e a ne. !M s#ript. 0ame it :8nemyAnimation;. Add this #ode:
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" using UnityEngine; using System.Collections; public class EnemyEnimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; %animation+8Move18/.G!apMo.e 0 7!apMo.e.Boop; %animation.9lay28Move183; < <

Line E #reates a handle to an Animation #omponent. "his is .hat .e (se to #ontrol animations from a s#ript. Line 1, initiali es the animation #omponent handle. $(r Animation #omponent e-ists inside :=om*ieMi-amo;% and .e3re 9(st getting a handle to that one. Do.e/er% this 8nemyAnimation s#ript .ill *e p(t in the :8nemy; game o*9e#t. ?(t li6e .e said% the Animation #omponent that it needs to get on the other hand% is in the :=om*ieMi-amo; game o*9e#t. Aemem*er% the =om*ieMi-amo is a #hild of 8nemy. &n this sit(ation% .e #an (se Get!omponent&n!hildren to get the Animation #omponent properly. "he follo.ing diagram demonstrates .hat happens .hen yo( (se Get!omponent&n!hildren:

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 1+ of 31

Using Get!omponent&n!hildren% the /aria*le :Lanimation; .ill then get :lin6ed; to the Animation #omponent in the #hild game o*9e#t% and that happens to *e =om*ieMi-amo. Get!omponent&n!hildrenNAnimationO12 .ill ret(rn the first Animation #omponent that it #an find among the game o*9e#t3s #hildren. Altho(gh oddly% Get!omponent&n!hildren also sear#hes thro(gh the parent game o*9e#t% not 9(st in the #hildren. 0o.% *a#6 to the #ode. Line 13 finally ma6es the #ode play the proper .al6 animation for o(r om*ie. 5e simply pass the name of the animation .e .ant to play. Dere% .e (se the animation named :Mo/e1;. "he .al6 animation .e (se is meant to *e played repeatedly. Line 1+ does some #onfig(ring to ma6e it so that the animation .e (se .ill repeat. &n Unity% the setting for this is :5rapMode.Loop;. Atta#h the 8nemyAnimation s#ript to the 8nemy game o*9e#t. Ma6e s(re to apply this #hange to the prefa*. A(n the game no.. "he om*ies sho(ld *e .al6ing properly. <o( may .ant to slo. do.n the mo/ement speed of the om*ies.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 13 of 31

Giving &ariation To The %al! Animation


<o(3ll noti#e they .al6 in'syn#. 5e #an #hange that so they .on3t *e syn#hroni ed. Add this #ode:
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) using UnityEngine; using System.Collections; public class EnemyEnimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; %animation+8Move18/.G!apMo.e 0 7!apMo.e.Boop; %animation.9lay28Move183; %animation+8Move18/.no!mali,e.'ime 0 Dan.om.value; < <

Line 1B randomi es the starting point of o(r animation. Aandom./al(e gi/es a different /al(e e/ery time its (sed. &t gi/es a /al(e *et.een ,., to 1.,. &t #an gi/e ,.B for e-ample. 5e p(t that random /al(e into normali ed"ime. :normali ed"ime; #ontrols at .hat point the animation is on. "a6ing o(r e-ample% if normali ed"ime .as set to ,.B% the animation *eing played .o(ld 9(mp to the middle. ,., .o(ld mean at the start of the animation% 1., .o(ld mean at the end% ,.FB .o(ld mean FBP of the .ay% et#. )in#e the /al(e .e set normali ed"ime to is randomi ed% the animation *eing played .ill 9(mp to a random part e/ery start of the game.

'andomi(ing %hi#h %al! Animation To $lay


<o(3ll noti#e from the list of animations that .e ha/e three different .al6 animations. 5e #an modify o(r #ode f(rther so that it .ill sele#t one of these three animations to play.
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 using UnityEngine; using System.Collections; public class EnemyEnimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; st!ing animation'o9lay 0 88; sGitc 2Dan.om.Dange20H 333

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 1C of 31

14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 31 32 < 33

# .e(ault: case 0: animation'o9lay 0 8Move18; b!eaC; case 1: animation'o9lay 0 8Move28; b!eaC; case 2: animation'o9lay 0 8Move38; b!eaC; < %animation+animation'o9lay/.G!apMo.e 0 7!apMo.e.Boop; %animation.9lay2animation'o9lay3; %animation+animation'o9lay/.no!mali,e.'ime 0 Dan.om.value; <

"he animation that .e play is simply a string #ontaining the animation name. Dere% .e (se a string /aria*le so .e #an #hange its /al(e. Line 13 does a different .ay of generating a random /al(e. Aandom.Aange1,% 32 ret(rns a random integer instead of a float. &n this #ase% it ret(rns a n(m*er *et.een , and 3% not #o(nting the 3 at the end. )o this means it .ill ret(rn either ,% 1% or +. &n the s.it#h statement% depending on .hether .e get ,% 1% or +% .e spe#ify a different animation name to (se in o(r string /aria*le. 5e then 9(st ma6e (se of that string /aria*le to o(r pre/io(s #ode.

)ave *our %or!


Aemem*er to sa/e yo(r .or6 reg(larly.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 1B of 31

Importing The $layer 3d Model


0o. let3s ma6e (se of the 3d model of the player. &n yo(r lesson assets folder% there sho(ld *e a folder named :Player;. Drag that .hole folder into the Models folder in Unity.

&f the model ends (p .ith a gray #olor% the te-t(re 9(st isn3t assigned. "here sho(ld *e a material named :Player',1 Q Defa(lt;. "hat3s the file that .e need to fi-.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 1E of 31

Drag the Player te-t(re to the te-t(re slot in the material.

"he player 3d model sho(ld ha/e the te-t(re appearing #orre#tly no.:

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 1F of 31

0o. drag the Player 3d model into yo(r s#ene.

!hange the )#ale Fa#tor properly to ro(ghly mat#h its height .ith the other om*ies. <o(3ll ha/e to do some trial and error to find the right /al(e. Dere% a /al(e of ,.,1,4 .or6s fine% *(t yo(r mileage may /ary. Aemem*er to #li#6 the Apply *(tton .hen done.

0o. this 3d model .as done in a different manner. "he 3d artist .ho did this% #om*ined all the indi/id(al animations into one long animation. Perhaps the program he (sed to #reate the animations only .or6 that .ay.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 14 of 31

5hate/er the reason% .e need that one long animation split into proper parts. 5e3ll do that in the F?@&mporter settings. "he setting .e need to t(rn o(r attention to is #ir#led in red in the follo.ing image:

!li#6 on the pl(s sign at the right. A ne. entry in this ta*le sho(ld appear named :idle;.

<o( need to #hange the /al(es to the proper start and end frames. )imply #li#6 the n(m*er then type on yo(r 6ey*oard to edit them. "he follo.ing image sho.s the /al(es that yo( need to enter 1in a prod(#tion en/ironment% yo(r 3d artist .o(ld *e the one .ho 6no.s these /al(es2.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 19 of 31

<o( #an #li#6 on the pl(s sign again to add more entries. Ma6e s(re to type the names properly% in#l(ding the #apitali ation. $n#e yo(3re done% #li#6 Apply. "he animations .ill then *e split .ith those entries yo( made.

Illustration 3: I# you look at the $nimation omponent o# the player 3d model% it!ll have the animations that you spe i#ied.

Using The $layer 3d Model


)ame .ith o(r om*ie% .e3ll remo/e that .hite #aps(le and ma6e (se of the player 3d model instead. )o sele#t yo(r Player game o*9e#t. Aemo/e the :!aps(le 1Mesh Filter2; #omponent. Do the same for the Mesh Aenderer #omponent. )et the Player game o*9e#t3s < position to ,.B. Drag the Player 3d model into the Player game o*9e#t. )et the Player 3d model3s position to 1,%,%,2 so it gets #entered into the Player game o*9e#t.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +, of 31

0o. .e3ll edit the !hara#ter !ontroller so it ends (p en#aps(lating the 3d model. Use these /al(es:
Deight: 1.4 Aadi(s: ,.C !enter: 1,% ,.9% ,2

Again% its fine if the lim*s end (p protr(ding from the #aps(le. 5e 9(st need the general shape to *e inside. 0o. r(n the game. <o( may find the Player falling thro(gh the gro(nd. "his is *e#a(se his < position is too #lose to the gro(nd so m(#h that it passes thro(gh. )et the Player game o*9e#t3s < position to ,.B1. 8ssentially .e3re n(dging it a *it (p.ards% so the #ollision .or6s properly.

"i+ing The ,rosshair $osition


<o( may find the #rosshair ending (p too lo..

5e 9(st need to #hange the Player3s Loo6< position. Aemem*er the Loo6< game o*9e#t a#ts as the pi/ot point for o(r #amera .hen it rotates. And the #rosshair is at the #enter of the s#reenJ#amera. )o .e need to #hange Loo6<3s position to #hange .here the #rosshair is.

)ele#t the Loo6< game o*9e#t. )et its position to 11% 1.+B% 1.B2. "his .ill mo/e it f(rther (p and for.ard. "est the game. "he #rosshair sho(ld *e high eno(gh no..

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +1 of 31

Using The $layer %al! Animation


)ame .ith the om*ie% .e need to play the .al6 animation of the 3d model. Ma6e a ne. !M s#ript. 0ame it :PlayerAnimation;. Add this #ode:
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIis281e!tical83; i( 2v ; 03 # %animation.9lay28Dun-o!Ga!.83; < else # %animation.9lay28F.le83; < < <

"he Animation /aria*le and Get!omponent&n!hildren sho(ld *e familiar to yo( no.. &ts the Update f(n#tion that does things differently. 5e p(t o(r animation #ode in Update. "his is *e#a(se .e need to #onstantly #he#6 if the player pressed the mo/e *(ttons on the 6ey*oard. 5hen the player presses the 6ey*oard 6eys for mo/ement% .e play the proper animation a##ordingly. &n line 1B .e get the /al(e of the >erti#al inp(t from the player. Aemem*er the :>erti#al; #orresponds to the (p and do.n arro. 6eys or 5 and A 6eys for for.ard and *a#6.ard mo/ement. 5hen its /al(e is greater than ,% that means the player pressed (p or 5% so in line 19% .e play the :A(nFor.ard; animation. &f the >erti#al inp(t3s /al(e is not positi/e% then that means .e3re not mo/ing for.ard. &n that #ase% in line +3% .e simply play the :&dle; standing animation instead. 0o. let3s test the s#ript. Atta#h the PlayerAnimation s#ript to the Player game o*9e#t 1the parent Player% not the 3d model Player2. &f yo( r(n for.ard% the proper animation sho(ld play. Unfort(nately% yo(3ll find that the starting and stopping of mo/ement has an a*r(pt 9(mp in the animation *eing played. !hange the #ode to (se this:

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page ++ of 31

01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2&

using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIis281e!tical83; i( 2v ; 03 # %animation.C!oss-a.e28Dun-o!Ga!.83; < else # %animation.C!oss-a.e28F.le83; < < <

Dere .e (se !rossFade instead of Play. !rossFade smoothly transitions the animation to play .ith the pre/io(s animation *eing played. &f yo( test the game no.% the player sho(ld #hange *et.een standing and .al6ing smoothly. ?y defa(lt% !rossFade transitions the animation in ,.3 se#onds. <o( #an #hange this length *y passing a se#ond arg(ment. For e-ample% if yo( .ant the transition to last ,.FB se#onds:
1* %animation.C!oss-a.e28Dun-o!Ga!.8H 0.&"(3;

<o(3ll noti#e the transition to go to the idle animation 1.hen the player stops mo/ing2 is a *it too late. "his is *e#a(se o(r #ode still #onsiders the #hara#ter to *e .al6ing for.ard if the player releases the (p arro. 6eyJ5 6ey. 5hy is thatI &np(t.GetA-is smoothly transitions *et.een ,., and 1., .hen mo/ing for.ard. )o on#e the player stops mo/ing% the >erti#al inp(t doesn3t go to ,., immediately. &t de#reases slo.ly to ,.9% ,.4% ,.F% and so on% (ntil it rea#hes ,. "his isn3t .hat .e .ant. Fort(nately% Unity lets (s #he#6 the >erti#al inp(t3s /al(e .itho(t the smooth transitions:
01 02 03 04 0" 0$ 0& 0) using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +3 of 31

0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ < 2&

# %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIisDaG281e!tical83; i( 2v ; 03 # %animation.C!oss-a.e28Dun-o!Ga!.83; < else # %animation.C!oss-a.e28F.le83; < <

&np(t.GetA-isAa. ret(rns the a-is3 /al(e .itho(t the smooth transitions. &f yo( test the game no.% yo(3ll find the player goes *a#6 to the idle animation m(#h faster no.. Let3s #ontin(e the #ode no.. 5e3ll add #ode to play the :A(n?a#6.ard; animation if the player is mo/ing *a#6.ard.
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIisDaG281e!tical83; i( 2v ; 03 # %animation.C!oss-a.e28Dun-o!Ga!.83; < else i( 2v : 03 # %animation.C!oss-a.e28DunBacCGa!.83; < else # %animation.C!oss-a.e28F.le83; < < <

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +C of 31

31

0o. let3s add #ode to ma6e him r(n left and right.
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 31 32 33 34 3" 3$ 3& 3) 3* 40 41 42 using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIisDaG281e!tical83; (loat 0 Fnput.4etEIisDaG28Jo!i,ontal83; i( 2v ; 03 # %animation.C!oss-a.e28Dun-o!Ga!.83; < else i( 2v : 03 # %animation.C!oss-a.e28DunBacCGa!.83; < i( 2 ; 03 # %animation.C!oss-a.e28DunDig t83; < else i( 2 : 03 # %animation.C!oss-a.e28DunBe(t83; < i( 2v 00 0 KK 00 03 # %animation.C!oss-a.e28F.le83; < < <

For r(nning left and right% .e (se the :Dori ontal; inp(t in line 1E. )in#e .e ha/e t.o /aria*les no.% .e #hange ho. the idle animation gets played in line 3E. <o(3ll find that .hen mo/ing diagonally% for e-ample% .hen mo/ing for.ard and right% the player doesn3t mi- the t.o animations properly. 5e essentially ha/e t.o gro(ps of animations: the for.ardJ*a#6.ard animations% and the leftJright animations. 5e need to spe#ify that .e .ant these t.o gro(ps of animations to *e mi-ed properly.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +B of 31

Unfort(nately% the !rossFade f(n#tion stops all other animations .hen it plays the ne. animation. !hange the #ode to (se this:
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 31 32 33 34 3" 3$ 3& 3) 3* 40 41 42 using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIisDaG281e!tical83; (loat 0 Fnput.4etEIisDaG28Jo!i,ontal83; i( 2v ; 03 # %animation.Blen.28Dun-o!Ga!.83; < else i( 2v : 03 # %animation.Blen.28DunBacCGa!.83; < i( 2 ; 03 # %animation.Blen.28DunDig t83; < else i( 2 : 03 # %animation.Blen.28DunBe(t83; < i( 2v 00 0 KK 00 03 # %animation.C!oss-a.e28F.le83; < < <

"he ?lend f(n#tion on the other hand% .on3t stop the other animations *eing #(rrently played. ?(t 9(st li6e !rossFade% it smoothly transitions the ne. animation. &f yo( test the game no.% it mi-es the animations% *(t still not H(ite right. 5e ha/en3t spe#ified yet .hen sho(ld the animations stop so they 6eep on piling on top of one another. !hange the #ode to this:
01 using UnityEngine; 02 using System.Collections;

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +E of 31

03 04 public class 9laye!Enimation : MonoBe aviou! 0" # 0$ Enimation %animation; 0& 0) voi. Sta!t23 0* # 10 %animation 0 4etComponentFnC il.!en:Enimation;23; 11 < 12 13 voi. Up.ate23 14 # 1" (loat v 0 Fnput.4etEIisDaG281e!tical83; 1$ (loat 0 Fnput.4etEIisDaG28Jo!i,ontal83; 1& 1) i( 2v ; 03 1* # 20 %animation.Blen.28Dun-o!Ga!.83; 21 %animation.Blen.28F.le8H 03; 22 %animation.Blen.28DunBacCGa!.8H 03; 23 < 24 else i( 2v : 03 2" # 2$ %animation.Blen.28DunBacCGa!.83; 2& %animation.Blen.28F.le8H 03; 2) %animation.Blen.28Dun-o!Ga!.8H 03; 2* < 30 else i( 2v 00 03 31 # 32 %animation.Blen.28Dun-o!Ga!.8H 03; 33 %animation.Blen.28DunBacCGa!.8H 03; 34 < 3" 3$ i( 2 ; 03 3& # 3) %animation.Blen.28DunDig t83; 3* %animation.Blen.28F.le8H 03; 40 %animation.Blen.28DunBe(t8H 03; 41 < 42 else i( 2 : 03 43 # 44 %animation.Blen.28DunBe(t83; 4" %animation.Blen.28F.le8H 03; 4$ %animation.Blen.28DunDig t8H 03; 4& < 4) else i( 2 00 03 4* # "0 %animation.Blen.28DunBe(t8H 03; "1 %animation.Blen.28DunDig t8H 03; "2 < "3 "4 i( 2v 00 0 KK 00 03 "" # "$ %animation.C!oss-a.e28F.le83; "& < ") <

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +F of 31

"* < $0

<o( #an see its getting rather #omple-. ?(t all .e3re doing is ma6ing s(re .hen one animation is *eing played% the other animations that sho(ldn3t *e mi-ed along .ith it sho(ld stop. For e-ample% .hen r(nning for.ard% .e need to ma6e s(re that the A(n?a#6.ard animation is not played. 5e do this *y spe#ifying , as the se#ond arg(ment to ?lend. "he se#ond arg(ment to the ?lend f(n#tion spe#ifies *y ho. m(#h sho(ld the animation *lend .ith the others. A /al(e of , .o(ld slo.ly stop it from *eing played. A /al(e of ,.B .o(ld slo.ly mi- it B,P .ith the other animations. "he d(ration at .hi#h ?lend arri/es at the target *lending /al(e yo( .ant is ,.3 se#onds *y defa(lt. <o( #an #hange this *y spe#ifying a third arg(ment% the n(m*er of se#onds% 9(st li6e in !rossFade. A(n the game no.. "he for.ard r(n and leftJright r(n animations sho(ld mi- properly. Do.e/er% r(nning *a#6.ards and leftJright doesn3t .or6 properly. A#t(ally% .e 9(st need to s.ap *et.een leftJright r(n animations .hen r(nning *a#6.ards.
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 31 32 33 34 3" 3$ 3& using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIisDaG281e!tical83; (loat 0 Fnput.4etEIisDaG28Jo!i,ontal83; i( 2v ; 03 # %animation.Blen.28Dun-o!Ga!.83; %animation.Blen.28F.le8H 03; %animation.Blen.28DunBacCGa!.8H 03; < else i( 2v : 03 # %animation.Blen.28DunBacCGa!.83; %animation.Blen.28F.le8H 03; %animation.Blen.28Dun-o!Ga!.8H 03; < else i( 2v 00 03 # %animation.Blen.28Dun-o!Ga!.8H 03; %animation.Blen.28DunBacCGa!.8H 03; < i( 22v ;0 0 KK # ; 03 LL 2v : 0 KK : 033

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +4 of 31

3) 3* 40 41 42 43 44 4" 4$ 4& 4) 4* "0 "1 "2 "3 "4 "" "$ "& ") "* < $0

%animation.Blen.28DunDig t83; %animation.Blen.28F.le8H 03; %animation.Blen.28DunBe(t8H 03; < else i( 22v ;0 0 KK : 03 LL 2v : 0 KK # %animation.Blen.28DunBe(t83; %animation.Blen.28F.le8H 03; %animation.Blen.28DunDig t8H 03; < else i( 2 00 03 # %animation.Blen.28DunBe(t8H 03; %animation.Blen.28DunDig t8H 03; < i( 2v 00 0 KK 00 03 # %animation.C!oss-a.e28F.le83; < < ; 033

5e3re ending (p .ith not so easily (nderstanda*le #ode% so here3s some des#ripti/eness if it helps:
01 02 03 04 0" 0$ 0& 0) 0* 10 11 12 13 14 1" 1$ 1& 1) 1* 20 21 22 23 24 2" 2$ 2& 2) 2* 30 31 using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation; voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; < voi. Up.ate23 # (loat v 0 Fnput.4etEIisDaG281e!tical83; (loat 0 Fnput.4etEIisDaG28Jo!i,ontal83; bool bool bool bool !unning-o!Ga!. 0 v ; 0; !unningBacCGa!. 0 v : 0; notDunning-o!Ga!.5!BacCGa!. 0 v 00 0; !unning-o!Ga!.5!Stan.ing 0 v ;0 0;

bool !unningBe(t 0 : 0; bool !unningDig t 0 ; 0; bool notDunningBe(t5!Dig t 0

00 0;

bool notDunningEtEll 0 notDunning-o!Ga!.5!BacCGa!. KK notDunningBe(t5!Dig t; i( 2!unning-o!Ga!.3 # %animation.Blen.28Dun-o!Ga!.83;

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page +9 of 31

32 33 34 3" 3$ 3& 3) 3* 40 41 42 43 44 4" 4$ 4& 4) 4* "0 "1 "2 "3 "4 "" "$ "& ") "* $0 $1 $2 $3 $4 $" $$ $& $) $* &0 < &1

%animation.Blen.28F.le8H 03; %animation.Blen.28DunBacCGa!.8H 03; < else i( 2!unningBacCGa!.3 # %animation.Blen.28DunBacCGa!.83; %animation.Blen.28F.le8H 03; %animation.Blen.28Dun-o!Ga!.8H 03; < else i( 2notDunning-o!Ga!.5!BacCGa!.3 # %animation.Blen.28Dun-o!Ga!.8H 03; %animation.Blen.28DunBacCGa!.8H 03; < i( 22!unning-o!Ga!.5!Stan.ing KK !unningDig t3 LL 2!unningBacCGa!. KK !unningBe(t33 # %animation.Blen.28DunDig t83; %animation.Blen.28F.le8H 03; %animation.Blen.28DunBe(t8H 03; < else i( 22!unning-o!Ga!.5!Stan.ing KK !unningBe(t3 LL 2!unningBacCGa!. KK !unningDig t33 # %animation.Blen.28DunBe(t83; %animation.Blen.28F.le8H 03; %animation.Blen.28DunDig t8H 03; < else i( 2notDunningBe(t5!Dig t3 # %animation.Blen.28DunBe(t8H 03; %animation.Blen.28DunDig t8H 03; < i( 2notDunningEtEll3 # %animation.C!oss-a.e28F.le83; < <

Dopef(lly% that .ill *e easier to (nderstand. 5hen yo( r(n *a#6.ards diagonally% .hi#h leftJright animation to play is re/ersed. "hat is% .hen mo/ing *a#6.ards and to the left% instead of :A(nLeft;% the :A(nAight; animation is played. "est the game. "he player sho(ld r(n properly .hen mo/ing *a#6.ards no.. Unfort(nately% sometimes .hen yo( #hange mo/ement dire#tions H(i#6ly% the animation *eing played ends (p .rong. 5hat happens is the animations *e#ome (nsyn#hroni ed. "he fi- for this is simple.
01 02 03 04 0" 0$ 0& using UnityEngine; using System.Collections; public class 9laye!Enimation : MonoBe aviou! # Enimation %animation;

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 3, of 31

0) 0* 10 11 12 13 14 1" 1$ 1&

voi. Sta!t23 # %animation 0 4etComponentFnC il.!en:Enimation;23; %animation.SyncBaye!203; < voi. Up.ate23 # (loat v 0 Fnput.4etEIisDaG281e!tical83; (loat 0 Fnput.4etEIisDaG28Jo!i,ontal83; . . .

Adding )yn#Layer at )tart ma6es s(re all the animations *eing played are syn#hroni ed. 5e spe#ify , *e#a(se all animations are at layer , *y defa(lt. <o( may find .hen r(nning *a#6.ards diagonally% the animation may loo6 too e-aggerated. 5e #an 9(st lo.er the *lending of the leftJright r(n animations .hen r(nning *a#6.ard:
. . . i( 22!unning-o!Ga!.5!Stan.ing KK !unningDig t3 LL 2!unningBacCGa!. KK !unningBe(t33 # %animation.Blen.28DunDig t8H !unningBacCGa!. M 0."( : 13; %animation.Blen.28F.le8H 03; %animation.Blen.28DunBe(t8H 03; < else i( 22!unning-o!Ga!.5!Stan.ing KK !unningBe(t3 LL 2!unningBacCGa!. KK !unningDig t33 # %animation.Blen.28DunBe(t8H !unningBacCGa!. M 0."( : 13; %animation.Blen.28F.le8H 03; %animation.Blen.28DunDig t8H 03; < . . .

4) 4* "0 "1 "2 "3 "4 "" "$ "& ") "*

"hat really is a messy fi- tho(gh. &t .on3t ne#essarily .or6 for all sit(ations. &deally% yo( sho(ld as6 yo(r 3d artist to ma6e a s(ita*le r(n leftJright animation meant for *a#6.ards instead.

No -umping Animation
Unfort(nately% the 3d model for o(r player doesn3t #ome .ith a 9(mp animation. )o .e .on3t *e a*le to #ode that in.

Game Programming Using Unity 3D Lesson 9: 3d Models And Animations. Page 31 of 31

In ,on#lusion...
5e3/e #o/ered H(ite a lot for ma6ing (se of 3d models and their animations. <o( learned ho. to play animations and mi- them together. <o( also learned a*o(t H(aternions% random n(m*er generation% and the handy Get!omponent&n!hildren f(n#tion. &n the ne-t lesson% .e3ll ma6e (se of :ragdoll; death animations.

You might also like