Unity 5 Tutorial - Animation Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

cant watch youtube at work but i really want to look at this later at home... there aren't enough unity animation tutes! so thanks :)

👍︎︎ 2 👤︎︎ u/fuzzyluke 📅︎︎ Aug 19 2015 🗫︎ replies
Captions
hey guys real NBA TV team-up league asks me to make a tutorial on how to make a character with built in animations move using the animations that came with the character so like triggering animations and controlling them and stuff so we're going to use a really cool character for this so I'll create a new project you don't need any asset packages let's go a window asset store and we're going to get this really cool character that's free called unity Chan so this character here which is like really pretty good so import that and just import everything ok so first go in the hierarchy and go create 3d object plane and make sure its position is 0 0 0 and its makes scale X 100 scale Z 100 we got this plane and inside this unity Cham folder go to stage materials and there's these materials that we can use for the ground so I like top 5 so I'll just drag that on there and then let's find let's go and slide models and there's this unity Chan model just drag that into the scene and put it at zero zero zero super tiny you can press F when you're most over top of this window to zoom in on so we got this character then I'm going to go in the hierarchy select the main camera and change up z22 and it's Y rotation to 180 so now you can see here in the game muscle I'm going to change the background on my camera to a solid color and I'm just going to make it black so first we're going to need to create an animator so we can control her so in the project panel I'm going to create folder and call this animation and then inside there I will create an animator controller I'm just going to call it player and select unit each hand in the hierarchy and we'll drag player onto her controller also if you don't have any of these windows you go window and find it up here so if you don't have the window animator I have it right here but animator that's what we're going to use with with her selected this animator is linked to the the player that we just created and the first thing to do the animator is turn off route apply route motion uncheck that what that does is it makes the character actually move like the animations actually move the character in real in the real world space and I don't want it to do that so inside Union Chan there is an animation as folder with like lots of animations so for her idle we're gonna so inside these there's this is the clip an animation clip you drag those into the animator and just because it's easier I'm going to highlight all of those in drag them in but normally you would drag in the individual Clips like in this one where there's a crap-ton of Clips if I was to drag this in it would bring in all of those clips but these ones I'll just have one of them so I'm just going to do a quick way and drag them all in like that so all the weight animations but it's just bringing in just this this thing so there's wait zero one two three and four right now the one that was orange that means that's the default animation so if you press play she's blanks she's doing doing that weight zero animation also inside of this animator if you hold the middle mouse button that's how you can drag around you can't I don't think you can zoom out it's a little bit annoying but anyway so she's doing that animation and if I right-click this one and set layer default state then that's going to be here default animation instead so now it's just doing this other one so you can set that to whatever one you want just see them all that one's pretty sweet so anyways for my very default I'm just going to use weight zero because you're just standing there not doing anything so we're going to do transitions so if you right-click an animation and go make transition makes this arrow and then you click left click on the one that you want it to transition to so let's make this guy our default for now and then what it's going to do is it's going to play that and then it will transition over to the other one so if you press play it's playing this animation first and then when it's done it will go to this standing still one make sure she's selected while he pressed place Li you can see these you can see it's playing through this one just doing our animation and then when it gets to the end it will transition to this animation now it just loops there forever may Plus make this our default layer' state wait one but let's right-click all of these make transition and send them to weight zero if you screw up and you attach the name to the wrong thing you can select the transition hold control and on a Mac I think it's a squiggly thing and then press Delete and that will let you delete that thing just pressing delete by itself oh yeah that works too on a Mac I think you have to hold squiggly button when you press delete so what we're going to do next is we're going to add a script to the player so that you can tell it what animations to play so let's create a new folder called scripts and create a c-sharp script I'm just going to call it player and I'm going to drag it onto her on to unity Chan and open that up and so we want to link to the animator controller so public animator just call it annum and then inside the start we will have an equals get component animate or an animator so that just gets this animator which is this thing so inside the update we want to detect if you're pressing pressing a button so if input dot get key down so get key is if you're holding the button and get you down is right when you pressed it you just clicked it down once I'll put one and print I pressed one save that press play so if I press one and if I hold it it doesn't matter it's just when I initially press it so when we press one let's tell one of these animations to play so delete this print and go Adam the animator that we just added dot play play and the name of the animation which we're going to do let's go to at weight one so it has to be exactly the same so all uppercase letters weight 0 1 and then the layer the layer is this base layer will just put minus 1 and it will go to this root layer so it says base layer right here you don't really need to worry about that too much yet but just so it knows where the animation is located it's located in this area which is base layer that's what the minus 1 is 4 and then 0 F is how far through the animation it starts playing at so we press the 1 key and then in the animator that we connected it plays the animation called weight 1 at the start of it 0 1 is the end of the animation so let's go back and just play and if I press one does that animation and it started at the beginning and then when it finishes it will transition back to the default if we were to write 0.5 that would be halfway through the animation so if I press 1 now starts right in the middle so from 0 to 1 is the length of the animation clip we're at place we'll keep that 0 and so maybe we should do that for all four of these so let's duplicate that so I press two it goes to two if I press three it goes to three the cross for goes to four save that it's you're pretty good animations too look how cute I am an awesome kick it's pretty sweet let's add some more animations so inside unity Chan there's all this stuff to work with so let's do getting damaged let's both have one clip so I'm just going to drag them in strangely so we got damage zero damage wined I'm going to make these transitions so right click make transition click on weight right click make transition weight and let's do something to trigger those so underneath these let's add another input give input dot get mouse button down and you put little open/close thing so that zero means it's the left mouse button one is the right mouse button and I think two is the middle mouse button so if we press the left mouse button Adam dot play a so damaged all uppercase zero zero in that first layer from the beginning save that bam punch you in the face yeah so that's cool so since we have two damaged animations let's make it random which one it does so let's make let's make it choose between a random of two things so make a float we'll make an integer called n which equals random dot range so starting from zero and there's a total of two numbers so it's going to pick a random number that starts at zero and there's two numbers so it's going to be either 0 or 1 so if n is equal to 0 then do this one else we got the other number let's just copy that and the other animation it's not going to randomly do one or the other save that let's play BAM Oh Oh it just beat the crap out of you oh he oh so now we want to pass in some parameters some variables in to the animator so up in the top of the animator there's layers and parameters so go to parameters I click the little plus thing and add a float and we'll call that input H for horizontal and another float called input V for vertical and then go back to the script and scroll up and we will make a private float input H Y the slope input B and then we're going to detect those down here we'll go input H equals input dot get axis horizontal and the same kind of deal for input V we'll get access vertical and then we will go to our animator and go set float called input h2 the number that we got by pressing left and right and the same thing for B I'm going to save that and then when you go back in here this input H right here is the first this first word and then this number is the second word so I press play now if I press left or right forward or back it's updating those numbers in here so now we can use those to affect the animations so we're going to create a so right click and go create state from new blend tree select that and then in the inspector will just call this walk and then double click it and now you can see in layers we actually went inside so that's what that different layers are so go inside the walk layer we got our blend tree in here and we're going to do a 2d simple directional we're going to use input H and then input V so pressing left and right and then pressing forward and back and then in the motion list click this little plus and go add motion field and add another one and add another one and add another one so in the first motion field select a little circle beside the first thing we'll make that walking so there's walk animations we have back forward left and right so let's put forward there and we'll say we're going forward if we are pressing the second list of variables is vertical so if that number is 1 then we're going forward and then in the second list we'll go walk backwards and we'll make the position why for that minus one and then we'll add the third one we'll add walking left so the first number instead of zero that should be minus one and then the second number should be one so that means we are pressing left and forwards and then on the bottom line we'll do walking in to the right so that would be the first number is input H so horizontal so we're pressing to the right so that's one and then we'll do one for pressing forwards also because of how the animations look so if you press play in this preview thing and then you move your input vertical up just walking forwards but if you move it down just walking backwards so if you press forwards or if you press black words and then if you're forwards and you move left and right it'll blend between the left and right animation so you can sort of see up top here those blue dots are the different animations and the red dot is how close how much we're blending between those animations based on if we're pressing left or right or forwards and back so then click back up to the base layer here so let's right-click weight zero go make transition and click on walk and then select the transition and we will make that not have an exit time which means we don't wait till the end of the animation plane we just immediately go there but then at the bottom of this window conditions it's only if we are pressing forwards so input V greater than 0.1 so as soon as we press forwards you can go to the walk and if we're also let's make another transition right click make transition click there then click on the transition again and now there's two things here at the top so you can switch between them so the new one make sure has exit time is off and four conditions go plus input V if it's less than minus 0.1 so if we're pressing a little bit forwards or a little bit backwards just start doing this walk animation and then if we're in the walk animation so right click make transition and send it back select that one and make it not have an exit time and then make it have the condition of input V is less than 0.1 and click + again and we're less greater than minus 0.1 so there's multiple conditions it's either or we're pressing either forwards or back we go into walk and then if we're not pressing forwards or back then it goes when we're different pressing neither then it goes back here so play that so walk stop walking walk backwards if I double-click walk and see the blend tree Oh so walk you can see the input V is changing based on what I press in and it's blending the animations on the right there based on that thing we did earlier Bam Bam Bam Shh so what you get so now let's just make the character move a little bit so I'm going to in the hierarchy I'm going to drag the main camera inside unity Chan so the camera will just move with her wherever she goes and select uni-chan and add component physics rigidbody I'm not going to use gravity so turn that off then open the little arrow thing beside constraints and check XY and Z for freeze rotation so she's not going to turn or anything so now we go to detect this rigid body from the script so just the same way that we did the animator will go public rigid body pull out our body and then go our quality is getcomponent rigid body save that so I'll scroll down we'll go vote root x equals in put horizontal and put h x about twenty times time dot delta delta time then we'll do load use Z equals input V times 50 F times time Delta time so we're using that same input thing and just multiplying it by a number that we want to use and then we will add these to the rigid bodies so our body dot velocity equals u vector three so when X will use move X we will do 0 in the Y and move Z save that so now she's walking and it's moving and if I go the other way and I can turn move a little bit there so when you go backwards shouldn't really be able to move left and right or if you're standing still you shouldn't be able to move left and right so we'll go if mu Z is less than or equal to zero move x equals zero f so if we're not moving forwards then the left and right thing will just go to zero so we can't move left and right so now if I'm standing still I press left doesn't do anything going backwards and first left is do you think if I go forwards and turn Wow so in the animations here we also have some runs we have run forward run left and run right so let's let's detect if we're going to if we're running or not so in the parameters click the plus and add a bool and we'll call that run so right now it's false and if it's checked then it's true so we'll leave that false and then we'll go back to the code we'll go up to the top and we will go drive the run and the start will say run equals boss and then we'll go down here we'll add another input go input if input dot hit key so we're not going to do down we're going to do get key so it's like for holding this button down key code dot left shift so if we're holding the left shift button do this stuff inside here so run is true for holding the left shift button else we're not holding the left shift button run equals false and then will do anam the set pool run so run inside the animator set that to what we have run at true or false save that then go back here and this running the animator should become checked on if I hold shift and it's not if I'm not holding shift so it's true if I'm holding shift up above the wok here let's create another blend tree call this one run double click it add new motion field three times uncheck this automate thing so that you can go minus 1 0 & 1 and we'll go run-run left for my the -1 and run run forward run f at 0 and run right at positive 1 you can see her turning left and right go back up to the base layer make a transition from walk to run so right click make transition click on run click on the transition thing make it not have an exit time give it the condition of run is true and also another condition of input vertical is greater than 0.1 there's no backwards run so it's only for forwards and then right click run make a transition back to walk select that has no exit time is false if run is false we go back to walking let's try this so if we walk in that I'm running the whole shift and I like go shift walk let's walk left and right run left right she moves the same speed though so she should probably make her move faster while she's running so let's go back to the code and so right here if we're moving backwards it does that stuff so we can do else so we're not moving backwards if we are running if run because we set that up here if we're holding shift and you're not moving backwards then let's take move X and multiply it x equals 2f post is 3 so that multiplies it by 3's move Z x equals 3 death x equals just multiplies it by that number so now she should go faster I'll walk in now running it goes a bit quicker walking BAM and your face okay I'll just do one more so we'll do we'll grab jump zero zero and drag that in here let's add a parameter parameters plus bool jump and then we'll jump back to the code and go we'll go if input dot fit key see code box base so if we press the spacebar do atom dot set will jump to true else we're not pressing space and set bull jump false so for press space set it to true for not suppressing space decided to Falls save that so we'll find playing this press space makes that true just like shift so let's make a transition from bronze right-click running make transition the jump and also from the walk to the jump and from weight 0 make transition use the middle mouse thing to move up and so all of those to the jump and each one will uncheck has exit time and go add the condition that jump is true jump is true doesn't have the exit time jump is true and doesn't have the haystack okay all three of them are will immediately go to the jump if we press jump and let's also add slide strikes slide in here make a transition from jump to slide and make a transition from slide back to the weight so hard to see the diesel so if we press spacebar from any of these animations it will go to the jump and then it'll go to slide and it'll go away so let's just play this wah oh so it's not a very elegant transition from the jump to the slide so one other thing that you can do is if you select that transition from jump to slide you can actually make this preview bigger it has has the next time you can actually move where the exit time is so then make it a little earlier took more of a straight in from the jump to the slide and you can change how long it is so I think that's about yeah that's allowed you it so I did from 37% to 70% on the jump animation but you can do it wherever you like this is pretty good so you can walk backwards you can punch you in the face you can do little cute little whatever I can float because we're going to coat that in but walk forwards turn run and jump Oh BAM he never gets a little puncture in the face if you got stuck anywhere I'm going to put a link below that has animation control project saved so you can just download that and look at how I have it set up and look at my code or whatever you need and yeah if you get stuck links below peace out yo
Info
Channel: Aaron Hibberd
Views: 745,679
Rating: undefined out of 5
Keywords: unity, tutorial, unity 5, c#, Unity (Software), C# (Programming Language), educational, learn, game development, animation, japan, unity-chan, anime, model, animator, animator controller
Id: wdOk5QXYC6Y
Channel Id: undefined
Length: 40min 55sec (2455 seconds)
Published: Tue Aug 18 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.