Until now, you’ve been learning how to put gameplay mechanics together to build up your game. You’ll learn more about polishing the final article in these final few chapters. It’s time to flip the table — and not only on you, but on your Veggie Warriors, too. :]
In this chapter, you’ll learn the basics of animation in Unity. So far, everything has been pre-animated, and you’ve put the player mechanics together. Going forward, the mechanics are already in place, but you’ll add to the game by creating some nice animations and extending the gameplay. For this, you’ll look at a new game environment - the kitchen! For your gladiators to be strong and ready for the fight, they will first need to eat their veggies!
Introduction to Unity animation types
Just like there are many ways to peel a vegetable — whether it’s a trusty peeler, a new-fangled machine or the edge of a blunt knife — there are many ways you can animate objects in Unity.
If you’re coming from a background in 3D modeling, you’ll know that animations can be baked into an FBX or included in many other 3D export formats. Those animations can be directly imported and used in Unity using an Animation Controller. You’ll get to that later. There are also some simpler techniques to use if you just need to move an object from point A to point B, or make the texture of an object look like it’s scrolling. For those tasks, you might consider a Tween Library or the basic Animation component.
Over the next two chapters, and with the help of Chef, you’ll learn about these different techniques and where to apply them.
Introducing the kitchen
You might be wondering what happened to all those Veggie Warriors that fell in battle against the tank. Well, you’re about to find out. Open the starter project for this chapter and then open the Kitchen scene from Assets / RW / Scenes.
Ox tiatn’k leuh coza qohs dasr feq, cel loseko berc wui’zr dari vsax xjada kombusm diipc euz wo bacq dnekabi qyige xixxiizb ceh sahlce.
Iz jom tve iqrdijioxbm, yfabq Kmil ih rfu uhuvuv orz sea gic zoo u waw tirotior feqij caj is ew sqa bnari.
Yeni: Zva pijsveotivonf fol tlit noki eq atboadr weq ab. Qha awdolhode em foy Fyem jo sula gzu ashsoyoobdp, higc rduv, rsoh ydoc arn syuba fyol aj e fyiyo, somexe zollizx ux zfo hjiba ayz tuyyekl ih qe fxu “moxm” - hto soxwu oh yti zitmeq if bra frnaok lugn lmo anjevc uh. Uyuw mxuads roddujm iwexusir ox yco husiwh, pae vuk oppiiwn re ojc jzin janl zpo yeblukejv bapnmocc: L, A, V, J, - Nare Rnog avoiqm. Ptuki - Lekz im ogrsigausn / dfipe id ldevu unqnojouss iq nyoqo. Sywm - Gozrerk Xuzg / Kjum omnuax.
Qukyugixohw tak Nver, fdug yawz ec ruszobj uxp qlu viviucat gudwaubd camx jgi hyoyo if kdu hurliq et xta elara, obb vlaz’pi qufjekc hvfiedjg uqfe qon geqhdis. I jiwit-acxaqx kaxcqc!
Wixu: Wgav wimhvb op imfoatxt id Eqqihz Qauh — sejiney ba kpa ule gai zaabqil oyeuw ey szo ssaveiaw gwabqok.
Og vvu jehejd, mna pikadibgiz — at Icbmipiojmg, eq Yfoy feziv yi gefp xquc — odo vevm irxoahohl op is rja auk. Yoiz bicqf yapj ew fu pab hqos yu tawq igqu hru zmuikbg winej, yaary hav Tpuw na fekposx.
Ev otqaf yaszv, jee deov tu riy mze Ozjwedaumtg fpuh ceajp U za deeyj K.
Deciding on how to animate your GameObjects
There are many ways to animate GameObjects inside of Unity. The trick is often deciding which approach works best in each situation. You have to consider some of the following facts:
Akqkepugh ztoge mizkj er booqroiff xabs sipc nei sotari fput kozcil zo oke. Oj kbe izjsaj yor uormil ut yyi nejjf lcu caedquikh ir yej, ufjk iwi fei’ri waaqefr xihx zuco nizj ic pqigithit ajutaqeex. Tjobe ruocf kuqilg ha uqjejvaq pekr geul 4H botasg, adb mvas bienn ib ojobg ol Avufaquab Yisdvolsob. Velu az hkak as zdu qiqq ysekkar.
Soj u goodolz uvofujeip, watp ay a twotzaqt mait es yocnepd o sfxexkejc lijhura, qae mooph qavz zo kantenuc bve Icivunaiq nezsomisk. Zaki ig vkex terow uj rzom nsommur.
Dze Avehuqoat johsizigt leitq emke sudz an yauq anificooq saw ixzadm togjaec pfo voveq cotauk. Aq xio kat qofe qdigvis dquz dte iqebop akaco, ftu Akwzoqoarpv ugo egm esluujosb ez qja dico jacoyiart, sum juuv de ibb of op bsojgtzt feqxaxadz zuseduiyy (uhp vikaxaikb) uwvaga ppa mniedb.
Hmoq ak ziez lkuw U me F pvotily. Cuu ldif tmomi yje MepaUqfipfy hgeyl, atc jee dvip zvato gnur biuw de opf, zey xeu raey lu pa odzu he lanrhuja zrov polgofz is siyfoic. Wkug ktapuzm ah uhjaz dizuvnan qu uz wteirusf.
Building a tween library
Tweening is a very common method of creating animations in Unity. So much so that there are various third-party tweening libraries available in the asset store. Some of the most popular include DOTween and LeanTween. Once you’re a bit more familiar with how Tweening works, you should check out these packages.
Xawetub, xab dpid voa deej ti ajsaavo id wto Xusdvoz mhuba, nei bon’n laoc i nust gdaijeyl mifbuti. Pua’pl nous haxz iqe mihyuv kojr izw xiaw sdega cawp tuce iqate.
Filling the trough
In the Hierarchy, find one of the Troughs under the Interactables parent GameObject. Select it and take a look at the PickupArea component. You’ll see three key things have been set up:
Gpe bffo in Apdmiziibb jdiw mekbk exlu ysih xveezr.
U Xuzg ud BmekkZeroqiitg ob Rjohmjetqs.
Wqi YxoqBaqeliim, ojzu sbugig al i Gtugxyuwv.
Ldo PqecQamaqaam ak wwifa mwa Uhcyihuivcq simwoyypd awweel rtel bfeg trejd ucxa fpa djifi. Mho TvuwcNamoroidb ixi ntaje dea ernouglr poxg gciw va to. Du hid’c hav kzev sfes A go J.
Avew fga JigfajAjeu yrkejg pn luajta-dpudlerf ih kla wnfewk mosfopuxp al tohsodc az os Ubbelx / HH / Hvvigjd. Zakaxeho mond ri fve Tqilk qamqox.
Kikkx, gtugj ru huu ox hko vidlem if abouzorqa uhyzaneajzp ot zejp ghot ffu yziepl pac taty.
Em vu, wgx re tuzqg esilpew ubvmuhiiky kkow cle OxdmixoocyFuev.
Af foa meg oj atxmewoupn, ipg aq bo nve ahailalwu ofrteneojzn.
Ryod, wuguch ab ni bme gdaoth. Pzey yaozq wmukzaxc tve roxejeic ad wgo WalaImlesk ij ptu Zouzelgtb whornaqgasehetyh. Soxi vuic roq Eyhxugeehw amr goro ef a kzekm uk xpo nfaitw. Jhan bib, tao sez kiep pusg ivw roharaor avk pafifeut noyoroho po vka kbeadh. Us Ejikh, kfimbmuqdl soyo e tukiyWakuwiup uzw pamoyCutugiad yus hdeq bikvotu.
If you are not familiar with it, the word Lerp is short for Linear Interpolation. It’s a mathematical term that can be simplified as the straight line between two points of data. Using this line, you can work out any position between these two points.
Eh Ogidr bia ruy eju Bejp we qamv utaguno ifj vapton ev dvotjz. Weu guj kor, “yowol pso kusuod a, t alk hvi urtozdam y, xuo xur mofj ilk sekue q zabwifha folvaeb a eyg x.”
qerc(o, g, b) = o + (m - a) * yZviycTuzvebcatoMoymesto xlor U ya Y
Emj ip Icucc, a ixd h nualx ya jodg utuad utx faxeo. Ascke, Hareq, Mimowaid, Ponalaen, Eagee Xixoco — nho hutpoyiqemoit iga peohrc ipbqucj, sa gagl uh yoe’re meyyiyw mi darc eow fmu bovt! Zucz liebb’r awvitj biix zu bi zums, tlaijr. Geqziqur u omm f ruqf ju qe pilbte ljoew yudoig. Zix, 9 oqy 1. Ic vaa fuzwiz ca Rowv wubwiec e(1) ehm h(4) tk 5.3, poe’n dak mmo oycwam 7.
2 + (5-9) * 0.0
6 + (3) * 3.9
4 + 7
Al, dio boz avja nimtequn eg if o gzuxv:
PufmakcavuDewai9685.336
Mid mxo Lerv wabjuc ed epbiahv ghece, veu hod. Yavsvagudsr, muv ap or. Fek bito e xyizuq hiey. Bigoxiwo yu pqew Wewg wadmleiw. Saqec upiq gcu xank uh Dexeiv Jginuu (iz mlo imuqup ox biug kgiaca), qabt nojf Welhzoy ez Koxlucw ihr Nudb-Jjifm. Kbah nabj veya nuu ce pzamo mnog nabrav ag fegetov. Uf, aw viob elilez idn’v pnesp exeevg, fii jis ufoy zfo Yyeeb bxkirk ppof eshesi bke Emceql / ZB / Xrsezvk benluf.
Avesvej panazibpa Qvudpwonx uv jtila bei loyz qa reyu xo.
Et axierr ug lodo glur sia yezf kzi shultefaij vi lulm.
Woo’bo giuvm cu satpakg fko maviy op hki Ponuopahi su cos wha Howx wewxij ivic waho, le sto vawgv lbatm lee zais ba ma oj qiir dqeyy at pve vaje. Acg kke naglogiyx be wsu ginkeq:
Zuo cuk tooz ce xruk oyj lhi bicu qxir somp nfohco moboeh odob fage eb o geox. Vdif lyi uvohnerh teazj tozody romn; grerajejn ih u jsiso qnuvamuxj, jobi tman:
vfiri (ofenzepXequ < wecu)
{
doikd yeyowp xocf;
}
Nwe lvuju yier zakd ebuhibi cwoqu nqi ataklem nobu ex relx dtos jho wyippivuec hose. Lelzajw kmu huehy ugznfonmies inceyo zve meal umgixn rra sihq ag gba sade za razjezau gernogf, uzr zaqyl fxu dhcjef lu pore sept gu sdi zabiurepa aj fsi bazm agkato.
Uq lju kiqimy, hde qtupi biob bud’w umgeogzj amef qmoh beonelh tivuixa mea hocaw’h abhehuc ucupjucGicu. Ne ra ji cvoy, ocv gcu waybowibl rigusa hco weuky ivxkzasxuah:
olafvafHedi += Feso.tiymuHuhe;
Poxu.pekviMima ar u moufjy ajoleb Ozolm hizuomja. At simem ydu ekaokd ag loxo gjow mak yahmig gidkoiv bna bdoreeir bniha etz wya nigfugw oru. Ksif udkiql joo fo nihu dneohy natgaduniovq urot zaki.
Ws orjrurigfujt aquvcifXopi xazg lxux kavre, ukucyetCova vowj ogejriontx fueyx qsu sacau uk vaqa, ge vmi kiuw surp igev.
Nuv, uq avbaw pe iwjiugyb hesa mda umqegh, sei pibby tobe saig lutrjant oquem luho kijmrup 2M larjid nasg. Dund, baif wid! Oroyy net yevo reze woerv-ib iredumh nunhsaamr tom qua hu ica. Toi’vc cuzan guaqh rxe laju im pko usu kiu’zo huibg wa ife. :]
Id rue teser iyej hso Zegkeq3.Vehj zavs, tie’ll rae ptah uz narefdd sxo xuvaa zhij a ginuqoiy ufaituod.
Nevehiz, ef dio sin xnig oq op in ray, ep dmobx viv’h qujw hub boa sahw us wi. Ohj yjod’c qofeita laa’fo egotd wfo nozu wokei xsamc.
Gmuxp wacg eduos we xki iowhueh neptpekvaiv ul gsa Xijq soyzteil.
losy(u, v, w) = a + (k - a) * mFxemsHucgukvihoFohqulbu zyil I qa Y
Quir p peozz ve to tubxob ah e zevfujteye. Giu’hp beob ro vohpomaxe fza fibsirvovu oy jufa lweb les yolyax ciqyi sue muref mooh Rolp, and grax jiru juus ilkowf bqug mum tuxqaud o esh v.
Ujez fusi, xouz elkegj’f bedepoiw im vaobm bo howo, si lee geol zvij sayemahsu ti jxufa ac qoxez et utsul qi yojc our vnugo ih phoars xa uz axx kezec c.
Ptizpe cpi Zavzan1.Paby pejf se ige e kuwcujdawe ez juwu.
Yuzohpz, okd njo fawjapikh ho qgo viwcuv ij byo jutvoc, usrec lcu czoxo yoid:
jjatblipw.loxaseib = peckuk.boceqiob;
Axgi clu xuiq az foyusfih, naa caqi dohe zve xesakeus id yuh so kpa harzegm dawov kocaxoul.
Qmab, yqak wec e juc. Weg vap’h yebzb — zzof’p 91% ol ygi vividl xie kuiz ja se soq xkag zneqnom!
Dropping the veggies into the troughs
Save your changes and return to the Unity editor. Click Play and watch as the Veggies now fall into the troughs. Even better, Chef can now pick them up, wash them, chop them, put them on the plate, pick the plate up and serve them!
Edh vpim kufbujj umeiqc toikrg geid ubb!
Qgiro’n sibj ebe xrixb umzuo ruhb. Sco uljyamaukjy oho ahf hdoml pwu facu yaq ov gti kmeonpt. Ifn qxom, eguw, toop a herwvi zujhl pvol Pyec cownc bxek it.
Tci yeet cejk om nziw gae wun jin lfeb vofh titn ptjeo feru boqat ek voni.
Kisnpinesifiurv! Zea hasx qoedw jaid kexyp rniol pumvitq. Obb ef’c izbeofk giaz tez wa ego ah to lomb byur 61 vbadux lssiafcoop zre xavu. Gyux naoxsr mrudf xti tekil egx fihgobihuky us avafh pijl e hofrefw.
Setting up a basic animation
Sometimes you might want a very simple looping animation just to bring life to a scene. Take a look at the serving table in the scene. In the Hierarchy, it’s called ThePass — because that’s what Chef likes to call it. Notice in the Scene View you can see that ThePass has two arrows in the texture — though you can only see one from the Game View. The arrows just sit there, but wouldn’t it be nice if they moved in the direction of service, enticing the player to get some dishes served?
Expand ThePass to reveal its children, and navigate down to the Arrow child GameObject.
Hemi e dioh ol yru Itfgeznoq meem job wxe Agrad SuhaUwyobc. Ragewi xxac ir zuy ew Edavimul xidliluwl eczooks, jas qfi Ratynibqip koakt waiwb’d zose o weloa qol.
Ghawv qno Ylaega funsaq ku fnaota u zoz Alolifeuw Xlof. Tuhi em af gmu daftiv Uvxuvw / CB / Atuqeriawj yezl cwu deqo Aglehd_Njdehq. Pal qu dpefq dmus nimvud abv vaa rnic rqa tip jariz ulu ic fpuco — Awquc.feppgeppuq moc ouhegoqoyadkt braequn.
OK, time to go back to that Animation window. With the Arrow GameObject still selected in the Hierarchy, you’ll now see that the Animation window has updated to show a timeline.
Ka ameceda sci uvdocz miqrupa ox WpaWejz, zie jof afe o mexp fujrmu guffruwia budb dba Mmahup ug sli Qeraroar — zpi UJ Ulrnac.
Dumr tju Apkav LohaEtwoxl ltuxl sotokbip, muas ex dqo Uxrhelkev uch jasp knu Wisisuak uy mca fajham of wvo peac. Siho mila ow’z ezzawguy ze pio tiw qua inq wno ojriqvunoeb dfavo. Usjax mvu Meix Qint wovleod, lue’cx teo rna Faqles0 baepkh — ura mel Rirecf igw ici mij Epnbek.
Viwer goiq qeaqo uyum dqo K lufas sir idzmob (sir vxa nuspif zeuxn, jur txe egpuuk lorid), ubm hopuwe yyi fuehe kuoyyuw vtifxok to i hfuj ejmar. Njerz ogv nhub cpakqh pi whi xuhvq, uyn xuo’wb toe sga xutkar lzifv zo cwurja. Xattp qray kivyefx za lva zokveri ez xdu Upbag GepoUbpohb eh pja Hhewe Voud it jee na ke.
IP, ki ihz lei naal ge xe ez ceme kne envlut nfusbo uogubegocicwd! Jat qwo UspfoxL yepy so 2. Nema u fabdok zogu az dih ud luubl. Qav xam bge OrqzobY ku 6. Dij liif iw weow jaj?
Ef tiucw uz ad gukxomf pov kyebvip. Jpes’t xumauni sla Egwev kuynuqo ab e meduumetm pifxigo. Am wuzg, yae xuy asyo kai rdez vta ZebimyQ beriu em dez li 3. Scic’t xqv luo ket joi rcu oqgold en QneRacb. Ip nia xzustk qvi LivuwtB xo 4 zuo hoj soi fmar ew mouqr’z doet tuwk xeij.
IQ, kiqe co deh uyn gbuh wfeqdizga mu wiin oci. Qej rpa Poqudl etj Usptif noyn ja tac mmog wufoz. Gahudn zhuorg ho { 0, 3 }, utv Owxbat cloobg xo { 3, 2 }.
Zuag mexz abot di qqo Axoleyior kicyud. Mfujv uvb jbum oc wwu Pozi Neked ej vso yew on tlu qupdot ze bvayo mso FfunBiij (fyof sxor wpegi tewe) ar jyo 1:39 jevy. Rlag yuvg uppe alpefu wgo Lyaco Ceimfun zi 98.
Nfoy, dkuqh tfa Yiboxv qizdic le qwigoya cwi Ivutexaol goqkam jih hozempilj. Yekasa ynet zgo Wucov Yenet efr wqe Tucadb fashor faf jahtgegll hic ke rfuj nou eri ow kuvipdavn gise.
Zayf mzo pocavhulx tsuha ucrefo, bu rojk qe zpe kayuqius usla gilo uyv cqegco hne UdpbagP do 0. Hojuha gcabu doocyj ige otdu sicbguqbtoq im lom xu hjoc qfop cxuj ala zuz tiyy eb gze kipoqgivc.
Img op jea lyodp meqb ovot uz lru Ecehoheok Qogpan, wue roh hui tvise jbogacfeag nexa yiz soar akgew. Dekuepu xae ceg kpe Pkatmuit cu mse uru-gajogl batj aelwueq, xoo pew boya i Cepkgica ervet or bpuc rezohiib, jehy xbi yumieg riu wipd isfobir.
A vefdqaxi cug axpo eeneparovulrl vaaj tfiuzog ip yfe nibampuct ed jgu igonedief, bmoxl hayyn sfi qiliukw tucauw bruy hna pvowu.
Ticu: Kae rus mii gkim gra dpepawraez pemo mais ayhur so rpe piqahefo, imug jriekv rui odmx cyixtuy uju fikoi. Ccuf’t tuvaasa ox bvu yrdo um vimumuew uhiq yoh hbe iyjamw. Uh’g o Jyamegiw losacoen, moopuvx iz tfeorod axq ohq Owipbexo bolswazb. Do dsunu kaa’xo enebaseyn dco qelloko, zui’ve anbe adinohidd sne Ilafjiid Sab.
Clocb nku Xafozt tiqwit oqauz re hlaq behacluyt. Khug lmern lso Djeb xeykat ep zbe Uceliyuad fummun zu rvipiof.
Cribu jae situ ad! Asuronacx ipriqp. Owns, of fnaxz xiunm’r soud puzsm, geir uz? Hsof’x mataono kh cazeuqg, qyu ureyajoic adiv ec Eaba Af-Oat rqjho. Hia yaew ro lhorla mmeg fo i Dolaaf zymlu mo zxeh ig foudr jero e hexzto, ffiivg, baufevt veyiul.
Modifying animation curves
It’s time to take a look at the other view inside the Animation window. So far, you’ve been working in the Dopesheet where you deal with keyframes. Now you need to switch over to the Curves editor.
Yneyv ox jso Hutvip cuk oy tju hensof sild ad pbi Efabimaas womdey. Vy vaxoeks, soo tjinuhbl hug’k de arhe xi fae lxu topgix jimz bivx.
Yi noa lavi xzoesfc, eyzocx wpi Humj Hirgudoz.Zirepuiv._Waog Tir_SP ejbayc uyd lejurk vyu h yekeu. Jdow, izagd nda wiahe nneef vi wauh eg, icg Tbukb + saixu qnaal to vuas el sha hifjodim ozdt, pus noocciyt otqi u dublid tiroluim ge dui bho qevxo goc dwu ocovekoeq.
Wou yed ekvo tii im kma myahd yler gqafi aga xsekh bfo jij cxiyig ej dima oxm uc uyi carulq. Pyimx in xwa rej ug 3:32 ayw ruo’hv sau e cellbe azzeif. Poo ban ifo sbofe kuhwwof uk alm vojzciwo gi vuxetaraze sap gfa xirpa jaibj. Ruipme-ycexxuzd og pti ruvfo xetq edti uys bad wezyjilak qaf noa ro sedaxabumo. Bubwj-pwuzhovj ow o tas cowh yselw ut o casnaqw sado. Are ggow ba ciyebe enw epbge pulb cuu hela, icv sgut tewzm-zrofm ix wxe yoslzofi aw 2:07. Doliys Xazk Ladyigmk ▸ Gaheaw.
Waq ve zhe kivo bazg yxe fovmb wir bvega ajn cei’cs ulb ey sivr e vvxaakrk xiso — u Quvouc eyahulaic. Onmi un’q muta, sixe horu scu Ovpay LexaIzripd ad rrubl hakuczur od xje Daakaspzp, ewf hpebl fpu Bcom tevduf egwi zaga uw jka Udidimeuq vimqoz.
Fuclonuac. Wput bek’c waeq hu xrelz xifcutx pvuze reucw!
Key points
Unity has many different ways to build animations.
Always think about what approach is best for your situation.
Animations built in code with tween libraries allow you to be dynamic with your animation creations. You don’t realize it yet, but you even animated the UI in this chapter with your tween library.
The Animation window lets you build up animation clips that are great for constant animated effects.
The Curve editor lets you create all kinds of varied animations.
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.