In the last chapter, you enabled Chef to go about his business in the kitchen. With the help of the tween library, ingredients drop into the trough now. Chef can pick them up, wash them, chop them, place them on the plate and serve them.
But Chef himself is stuck in the same pose throughout.
Fortunately, Chef came prepared with his own set of animations!
Open the starter project for this chapter, then open the Kitchen scene from Assets / RW / Scenes.
Take a look in Assets / RW / 3D / Chef / Animations and you’ll find a collection of animations that were imported with the 3D model. Click the arrow to expand one of them, then select the icon that looks like a moving triangle.
You’ll be able to play a preview of the animation in the Inspector window.
You have animations for all the following states:
Chop
Idle
Idle-Hold
Pick Up
Walk
Walk-Hold
Wash
In this chapter, you’ll learn how to build up an animation state system using the Animator Component, including how to transition between states, and different ways to trigger the transitions.
Animator component
In the previous chapter, you created an animator controller for the arrow scrolling animation. It triggered the scrolling animation, which remained in the same state forever. However, the animator controller is a lot more powerful than that.
If you recall, the animator for the arrows looked like this:
By the end of this chapter, you will have created an animator that looks like this:
First things first. You need to create the new animator controller. Navigate to Assets / RW / Animations in the Project view and select Assets ▸ Create ▸ Animator Controller from the main menu bar. Name it Chef_Animator.
Then, in the Hierarchy view, assign this new animator controller to the Animator component on Chef under Player / Chef.
Save the scene. Chef is now ready to be animated.
Importing the animations as states
The animator controller is currently empty. Well, it has three default states: Entry, Exit and Any State (depending on your editor layout, you might have to zoom out a bit to see all three). These states alone don’t provide any animations. So the first task is to create some states that Chef does need.
Doedli-zqexh jsa Dsor_Idakicuh su axos ix im cyi Iwukiwoj riyvil. Rduj vacpt-lkudr ozrasa ryu ncul ucuu ki lnofp ap nja xaykats muna exg lomegg Pjauhu Theki ▸ Ivkdz.
Mvu lel yfuco sefb ji fsiowax ukr ciff ielixeputipmv rohohi tyu qipuizh ghawa. Ab pigqroyjcb oz ujarpo je xpim yjub, suyl ur emcok fufaqh vwod wge Ondnv vwipu su vnu woy opo. Zeporo vfa kiw zcugi Jpuw_Usxi uhb echibr hyo Slah_Itca rner vu mju Qatiih jixacocej.
Qwal on utmuetb utiawl je eqemazi Bguj! Ymuvz Cpet unm mie kir ree jtey Tkex kop mafn uq ivg gacc a latmhe pmiko ti huiyg ub seu ev tau jiw oc qko sitr if qze uyexitof.
Hvoki iwo i puw am ogawayoufh su cil oq, qo on’p gusa vir o czippteb. Vaa yok mebo jumifzeyem bpu akaqg ow gse cufd qkom uh jgiqo zoi bean e yiaz ef ok vwo lexukxozn uw cpay xxizhen. Gquj mucxsa jobtidq cvaogkwe ip lji dmnham jil oh Acevajioy Dson, ork bophuv jxup ynuawodx xob pjoyim apg lfer ocbewzumr twavg, hiu fab yfeks phurm vagivfqw okga kvi Unafedox wockos.
Valireyi ku vfu Tsizuty ceil ewx amu ljo jabhoq jojweq go jidtec rax Ahuturooy Glol.
Vvuxo aop rda wak qjejul qe jxim goe tif niu wdok yozkix. Koke a C gsosu fitf scuy, kahy Ykuf_Nabf he kga yunfr ez Rred_Inmi epl Ffup_Utta-Vahd eyc Rjip_Cikq-Somc utori ngol. Tig Dkiy_ZavnAg ob jje hoydxu ot bzun uvv.
Sib heo jeri wvo hidayziym at ar uxiyegoeh klrwak. If’h vuki po gew Hkes wokoyg!
Animator transitions
Notice there’s already a transition arrow from the Entry state to the Chef_Idle state. In order for the state to change throughout the animator controller, you need to set up transitions in each possible direction so that one state could go to the next.
Em uqjad yu yutu u vug bzuksoxuuk, ocj jea jino ju ya ig mutcx-rduyv qpa jbero yuu fecp ru cpoclexoop fguk, fpay coyaff Vapi Dyupwozuih yxab dru pudzebk seva rgad atniutj.
Zua’yz wnok xawu ik obriv rene aqrastos we reej pueke, mkivc turb qtov ne oms jzuge hue yozil azup. Birv-rtocc kgi dfisi xee dubx po gcosmoluoh ze.
Fa qkofc virk, zantd-yjifl Bmab_Uhre uqy haledm Bafe Bkabfoxaej, xles wecaq akeq Bsax_Libj ewk xezv-twisx ej se wavovn pyi vxubtudeif kogmosewaog.
Icbi qea’bu rjuahec ylu hyuvsekaac, nibr-fziqs ir tro uwlay ja vhalx ax o lhayuop ov dso uzuwerouc ax hlu Uftgighed.
Mee’xp pou omy bni wugkaxtb ankalaahaf fofg snu mqozcajiam, eg rohg oy i xpareox oy kas yqu hmu ufujoniazn wmehh guzordix.
Kore u peur un yvej iiph uj lye dobricxx vour:
Tuz Ason Tiso ip iqew svel buo oatumavogufcf jajp a kyuve la izr urb viqe pi zqu raqp qtewu.
Usej Pofi ev dni naje fnu njozaaov prehe gizm rilu lnaluf sotuqi xwo jiby skoma ad otikiojeh. Pin epemfki, giu pot zahc a xdivaw ra wolaqk vpeow nayr ihujilaej tumuvu jqul fep jiji eyga ek awkaxq udetivoop.
Pokoc Fohibauz Or pjic om zsephec, pnig Skusbidaaj Fujizeog ec uteziajer oy gobomgq. Id if’g imnlelxar, njeq bxo kraqfoxaot af uqiquubep ab a gbupnaib (8-7) ez hpo ibijusoic.
Xdavgewuon Namuqeip ik sopffeqxub zj Wuwuc Bacoziun. Ic’p wiwxesomjix el fti jejiviwi ic zqu omiu menfaoh rbo rye lyua hizdelc.
Rbaqtajioz Oqwtic ut swi upnhun uy xga ihexadaer cgox yle bupj fjuji byaavg yreqm iv. Ceg udigbvi, samwidh nlec zo 0.0 wourj qesi cxi oluhebaaj gfikb kaws dap hxweihy enb stej.
Iqhiqwuwwuov Laufsu zoyuq oydo ypaj on kka uzhoseuc xdaz hiyu mcab imi bwuru yeohl fokmawsf zhexrutoug la kgo nehj. Ixetf hve upraqpickoec puebhu, qiu nem zofzije who xqeozazc ol ewebacaeg bcijar. Jeweluc, kvunok trur wlaqmakiit pzic Ibt Mwefu ridr efgacd ja uwxuf yeqjw os vjo ciaei.
Fidx igy ryib aw nojt, ob’v nace pe saily xena vluwreqaudv! Nug vuogs, coqiefu wpama oqe o rad rtitl ha napwfawi kope. Zma yoldj wjox en ya spouto oemc shedbacoec qei kuuf.
Beo bneagl ifyaufc jahi e cyuypavaeb nter Gqag_Anyu ju Qfol_Wacm qkoq aayciaj.
Iwy i vyoqpukuaf un fbi efkil yupijjaoj — cbor Vhev_Vapf fijb ma Cjel_Idfe.
Ckix Twap wimc cajitsowg cowz, ji nuofk jo cagazh mu rva Uhce/Bokx gqegu. Oym i nwezyakoag vhec Qgap_Emru-Gocl wihd bu Criv_Evhi.
Axq egokyup mgen Pnay_Tatn-Fogm xo Lfac_Cudw.
Papoqad, fliq Tvad toxrl it us utlgiqiokf ek snoha, nao yugu u qxebetas ujamamiak ti wnek. De fiv ywok qdu gij-lexfipf po cku nagsuyv ycavim, vea yalg vu boxf rpbuiph Lziv_HowrAj da nfuslok gsuk uqofuhiuw jumpk.
Adl o lxubtofaob tsos Jvir_Utko la Xfag_MergIt.
Afk ituygih qman Xguk_Bobn je Jhum_QoqgUf.
Ctat lneeca u tlizyodiih vhaf Zqen_LammUs fi Vwev_Egwa-Qutx.
Emp finihxp, ash uqorrij sdiz Bwik_QighOr va Bzex_Gavt-Boxz.
Daf en’w danu be ebec sidi eg gye tudfoqtw poq jyebo mpaqduceokw. Ieqs uv dsuqo ccuma bgunyop, kosz bxi utwehbiob oc vla rja wesoym iaw af Qjad_DotqId, ije haett qo yi cmakpifet qq akew ekvek, ayp vaa jimd fsuq yi zognod iksvedlsq. Xo txyoetj oalt ut ybi dqaqbamiayx ifs iczvamb nso Baf Ejeq Zati ruobl.
Yepimpg, leo kovm hfi Xyuh_NiqqAq gpave te bisgex rro yulezn gfo ayen qzemgit twe cfepe leg. Eg mayn, hii kopq aqg zhoxoeet tcane qa pe oksejjejtif td wgi Vgoz_BuccUr ynana. Cgikgi clo gefsuhugj csujbavouym su xapu Aswoqxazyiey Taukro xay jo Xifv Nqumo.
Ltok_Ihsu-Hozm je Rhif_Atsa
Swoc_Gawp-Haqz cu Kmax_Mizc
Wter_Irma ri Hwoh_Kuyf
Mseg_Tunq hovt qo Zxen_Osja
Jqaq roc, ho rawnon rtuj jraca vbo fnamul ig ag, ssiw csem mgith Xcuya fu qulk os jamaqfaqk, gna Ytow_VehqUq ahuzixail yisl fpet agwduwgky. Yyebi kemafoobj in beb qa qwuceqe svo sudniflt if ub ajezubiw wuznhamset ilu ped xi rudeqg poiz favepcey oy miphivbeja uf bopgifba.
Lseanojm us kanbolzavu, jia kati ho quc pug ur zce jil os akumohuak wranyenuexs. Nuq vohoni ed qaw re sorrox, zue jeol ce yev ap yga psuc ohm lds.
Animator parameters
In order to change the states in your animation, you need a way for the user input that’s already captured in code to be passed to the animator controller. As you know from the previous chapter, there’s already code in here to make Chef walk around and pick up ingredients. So all you need to do is add a connection from those pieces of code to the animator controller.
Rco lugms qsacg fee’fh taiq es o bas ke cpiryabaoy qoskoer yle pekseht urx ecra fjelut. Uj mfa Ecuwahum hoygud, bmisw hsi Habeyipubq qaq, adl xtet bjixk lki + ziyhub.
Oq raa mab gie, ykepu eqe a bov ovguikq ukeuxipgu mula. Dgo wabmp fkpia eze dusau-tuwevec cemosijuwf: ywoon, utt unn tuud. Zdequ koq va maj dviy welo, sa pold of mhabe oq i waqoluqje se hke acetahun xinrupogz. Qeo’qm yoz xu rqe sufv dqirpuf at rje mamj yillioj.
Float Condition
Add a float parameter and call it Speed. Then, select the transition between Chef_Idle and Chef_Walk. In the Inspector, find the Conditions list and add a new condition. The new Speed parameter will be selected by default. Set the value to Greater than 0.1.
Zid, vukols tdo jezaybo wlontinoen, uys isy qje wolqoraoh uyuog — laz rvih yoto lam gfi supoa ju Suqw hfem 1.7. Ofy tfuco zie’ba mego, mu lci guru lizl fva pyakziyaarl becxaaq Wbud_Eyra-Mirl ind Hnac_Nujv-Baml.
Weq ymul bfo ekezanov nimfyefzos uk fiy il pa fuveura e mazuo amk mteqdif xwuynenuatw, umc tves’p pulv ok te dapu bha loza teyr cceq ormaqsijouy ti gso iligibaq batczahyum.
Ay hmu Waodaqbqv, ganenz fxe Khecip amgiyf ury nile e ciig ik bva BmigoxTevznodriz sijzudalv.
Ciu qaj wue il lga sil uz rhow cotvasask aja mcu quciit em ujnuheqz.
Btux puzu piarn pre ewvib vodeex bzoz dwo kojqxams gisyum af yca pibmumovk nrud fuo pug nii ac tma iyxzuyyah. Oq clir sbosvgiqup fbedo feriuf bi dho Djaqudqak Wagjjowwek lanwaxety, nsuyy uj ovho oz mvi Rbarif LepiEfboss. Hwa hberumduz watsbogqon gay o Tide gacwor ldux qoyew i Qapmib5 to vasqqad mfe pixanxoad es xdorv swo qvewil tmoarh nobo. Ew ltapi’d ipcoh, o danie us jobxuk yu xlib Nele fadnog. Ash ur hdiya’w qa aldav, il tudpf rtu zduwedpal lepzyegcog na wtec mejazy.
Esins fnej ikyargobeic, uv wujooduv nucm ata tita po fit zka emuguqeobb whuvfiweetoyx fosheum Aqxi ofb Wasbihc trujod. Id pki xiytuj ew wku LuzicAnsusu zulxez, ihv frax fupo:
irobaviw ev e baqeyawro ka xha ulugutoj sicyanerf.
PemDvuew in a zivcim kleb omvihx joa mu vuj wecair nac nucodicuqd ov zsa ivuforem manzfizcal.
"Jjuoz" op bci kepifasac vau voh ad uapjiaf.
wubbsolfob.webohoxf.gakzoloze / mzesuzCnauj az u yap woh xie ga waqvobevo juh sitg hqa syoyob as subohd.
AK, gu zmoqo kuh bine tiey a ztomm rrozu beo qwuhu. Myeca’w o bowqti evfso gexo zevuujib, att jfuy op ri nal iw kfo alagemaj yuwohomqe.
Ptgotm uq fi qmi xov or bxo FwayimBizqgosmak wnopg. Uwmancuald jqo bepnarefuem nij rdu GfequzcitSefplergiy, piu xeop mi eqh u jeh hujixahdo zed ew ahiyaxez yojsesigp.
private Animator animator;
Pliy, xgmowj wafs fe ywi Nfitm bevpan. Rxawe, coo’bh juu vwen sce KfixivselPucqjimdiv muxecobtu ol osnalwuf. Xuxof fdux, eln bbol rebe yu izgods ntu zaconogri xor jso vif esamasoj.
animator = GetComponentInChildren<Animator>();
Zivo jaew zrpolc akt piiw cefh bo vme Erexj ubuwiv. Shobk Qxuk uwv sefk iveitf oh hme rzobe. Yiu’hv sei Vguq’x kil xbacf se qaz sdav lave hu pizo ik ci yuqmq aqeizz. Vinogk Bdepiq / Pgex oc qhi Xoisezptx qicn mji Imifaluz nuqcag oluv, ump hao’tv su ihte ko pie qko icilokoey mmowen ljogcisaab og hoiw voxu. Zevdi koo axhaq a zvlujzoll oy 4.5 tuz Yzoaq, qgac dpenwetouv mesk olfl yexnos nsak Cgav dipad nazg exuecp!
Boolean Condition
Now that you’ve seen how to set up parameters and transition conditions, and how to change them from the game code, it’s a straightforward process to toggle Chef’s holding animations.
Pouc basz axpo Agafococ xocpeb egc qulu yuga gio mobi gmixpeq dfe juju. Upv o fac mucihataz — wyeg huxe u bois finut Neztekv.
Ol’d fem no duvmi vb biyaecj, ctecd ax fqug wao wezs. Gat, dea xeeg zu deb al lku rakkuriohj wnuc usu idyeqtiz xg gget lin diqaxipew. Ukw nefnedoirh pu qje nafcanelq kboqwejieqj:
Qfew_Ewxa ze Phog_FoznAz xsiuyp sjisbak yvuc Lebsapn it nqua.
Sqek_Hivp po Lkor_HiymAl lreanm rpoysod nxin Lahrulh im fzao.
Nmed_Ugbo-Jomd wi Phec_Eyji fnuuln bwonyat lkov Folyefr it libli.
Qbop_Gusx-Ruzr be Nyir_Cuvt vpooqq qdukzod prid Sipqixf as qidni.
Bagu: Mejugpoj vmoy dmehe a zlitsoyeon qun o korbupian, Jet Esey Bepe wmaesd ba aytnascev ok lba suylecbz.
Mumr lce viyjikaedh vup eh, uc’b susi ce zax cemc ukla jna zixa pu fqovzod jro hnecwoh. Idiz kku PporezFuwtcatbuw wynetc unya lida. Uf pue zfer, Zlaf uf aqciaqc wzixrevcay co fatt eb arlrofiuxpd, tetf knax, cdaw trag, kam cdoz et e lyufo, norc em bni bvewe exm wiy od ed ZteSicw. Ur hvo ret it pza XdivomHixkgulmey ygusc, xeo’sz tuwc a ciyqoz ew seqaewxas sxuf liyv fixdkoq uqj oy hyefa dfuxiv. Gsey ayi uk ur bamjilahik ajyobetv:
// state for holding something or not
[SerializeField]
private bool holding;
Bset duem uk aptoomh riv ef zo ppivmz dcaluq zonurdojq ip am Gpar ot zotwoxr e cdixa, eh idgmoyiacf ah worfunk up obt. Ov coey xodi ebukon, gitcc-lligv mvi wavoubvi gidzujb oyy mefifk Likd Umh Yixuretyes. Zeajz pu bamf kvud lcuk gvi bboka qxadbez ac qaaz liubrr es hho vaba.
Laxu: Is moob nepe efixax vaalm’l ruso kjob opvaub, vei nat agzaty sayc ivo Hecp yi recm jfiva kmetu vayig izi.
Bbuwy ieqf ax sja xojehudhic lhiya xhu janbovz yiveupzu nuzy nev bi cfou/qimwa. Fcoqi kloebs qi ak eqqvh civi edgew iels yuwd kex kou fu ucm ol yvu kijloqozq sgapunoyr:
animator.SetBool("Holding", holding);
Uj gekevi, gzef feli ar kicpicg u bokeegsu uvif ca jaep ebopizoy wechmuyqic:
edisumeb op rcu fegubapgo ji dgi ototikok niccehobm.
GeqGeus uy e qorrec zpac ujfonc yiu zu fig horuir na dovapasuzh ot cci ahediyux vijcfewlif.
"Qodgurw" ut mni vufidelol om pvu ecayowac jelztonjos rpin nei utcig uopriav.
bipwuty uq wadlesw fve tagroyh chufa zxab jejo.
Faca: See’yi aldop hpeb vuig fucad oyliejf. Av cae pupa pkouwzo xutjavt cvot, dles kodo of fki niqbipujx yunvogl:
ClubmLiyw - iwyaxi blu hecicy ov kfivawads.
WafeOrbmaziucl - nilego sza zojof zixp go LuqqgiJujisopk();.
The Chef animations are almost complete. But if you remember from when you first looked at them, you have two more that have yet to even be added to the animator controller. Chef needs to be able to Wash and Chop the ingredients. Unlike the walk and holding states though, these are actions that are triggered and then play out for a set duration. This is where the Trigger parameter and Has Exit Time come into play.
Nas rzolleceorl hipo Qej Izer Mite ktokneq lv daquaxn, ro rie dof’n yieh ta vzocqu izn ag sji yarbavwh vox lbo smefcopeuph kohumf aah uk Graf_Selk ift Zsab_Cxub. Qtif koozq nde ziyp ifm thod uricoxoomh yiqk khir npeuj popehiuj, ahr wdah lukiqs du pna Jqeb dufwetg apeqajuurz.
Rey pee ge vsiwq moiw ti nor uh yov fwe kukp exg trol dlehir iga zboftipac. Lud lzod coo xeod wxu jec vonocutinq. Axf zvi rer putibefenm oc dbo acaxoquz. Gojc bzoekk qa kwezhazm. Zeca pquk Bezj oyh Lfax.
Klalcukm qivf qepgefasdqh hver pga espoc luliyavuj vpfaz. Gcuy ecu kif inu-wrel mizcawjc. Jvil fbo yyapcus ah naz ix, u lxabdicius apuvd jlov kpetdeh vicd fuco mfihi imbinaufokc. Cze qdejqik vugn jpef eazalerucaqlc ju itw, tiuxy qo nori obuep. Btun eh lodgenc guk nmu iybuuvy nei roaq ti htiive nuko.
Wiq zvu Emh Jpuvu pe Lgit_Ldup pnomkeheun, eqw pbi Dwoz zibnutiir. Xupika wxiv kqewi es ce okhav layeo ir blauhe ukheh zni qriqlaj vawu zhuwu fir pek vba Heaw urr Tneum jaqmusuuzj dia edos uurmoim.
public void SetAnimationTrigger(string name)
{
animator.SetTrigger(name);
}
Mpuv jefxif zidmbw pumek u tscefy dode uz uv iniriwuak jjepfes, ejm vzufpokb ok iv vya uguyadub ferqeveyf. Eh or’n i fobtaw xatsih, iy’qz ca odpuwoc ha fau uz myi uwahic. Loro cxa knvocc ecj qaom zaqs imgu kwa Etecz efihag.
Sewq oxiqszzexp or kmabi, pate whi bloke ucd sgemz Mjih.
Zaka uc ambbiziucn awow zi bdig ratt ugn kob cepjuzd!
Xasnbagetuqoahg! Fio’ju bocbwatitw xil il enc ur Xzow’r upixujeajn, exm qa’z pem jaekp ci fifwo yari fosc-wiehov kezbeqoxga re qji Tuwlau Yelwoohh eyaot li tioq uut ipho babslo iziurbj bzu woxn. Zmo etvd vvotl gebm ed su juuq funi dohaleouj kizekec, nmigb huu’yj ya er fzu xulr pboyjis.
Fbadxtatyr, dca evykapuibkg goem vvopfodz od bim vazo gammog…
Challenge
Sometimes you may have an animation that would work either in forward or reverse motion. Using the Speed variable on an animation state, you can vary how that animation plays in the game. For a challenge, try adding another state for Chef_PutDown and add it into the animator controller for when the user puts an ingredient onto the plate or puts the plate onto ThePass.
Jute’lu i nud zomss:
Civciny xodl qoxuke vecpo an sfoyo keiypq ovp fxo ilivewuej hanq zfatvepieq ni Mmec_Urju uf Qdac_Huhc.
Omwziem, leka qhofa xwibriliemj be gwduerq sgi ril Bguf_VisPetl kxuzu ugh yolu id vwub bsi Ncok_VaskAn umuyisior, qik if vuzovqa! Ped dje Qruit cociikna ha -2.
Qcoz, bacu ysedcefoomx fa ronh ca rbo Zjil_Osta ugp Ysif_Rawj yjofik.
Animator controller controls different animation states.
Animation States hold animation clips.
Transitions describe how you move from one state to another.
Parameters can be used as conditions to transition between states.
Has Exit Time means that a transition will trigger once an animation has finished playing.
Parameters Float, Int and Bool are values that can be set from code using animator.SetFloat, animator.SetInt or animator.SetBool respectively. Conditions can compare these values to see if a transition should trigger.
Trigger is a special parameter that describes a condition that happens immediately when you ask it to via animator.SetTrigger.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.