Mechanim & Mixamo in Unity 5: The Basic Basics!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to another unity tutorial today I'm going to go back through the process of getting a character for mixer MO and using the Mac Anam system in unity so I have another tutorial which is the basics of Mac Adam and Maximo but the issue is that about 15 minutes into it there's this bad editing cut where I miss out a whole heap of steps which is causing a bit of confusion so I'm really sorry about that I'm not sure how I can actually edit an existing youtube video so instead of just going to create a new one and go back over these items step-by-step so first of all I'm in mixer mo so mix mo comm and I've logged in now you can also get free characters and free animations if you don't want to pay upfront to begin with until you see what you can do with mixer mo but if you're not an animator and you want to get really high quality characters and really high quality animations then this is the place to go it's a lot less messing around inside 3d modeling programs if you're not familiar with them so we're going to go to the store here and I always start by clicking on characters and it'll give you a great big list of characters that you can pick from and you'll find that some of them are free for you to use so in the last tutorial I used a character called Remi and he was free not sure where he is all here is here so this is Remi okay now a character I love to use is sporty granny and she's up here so I'm going to use her but it doesn't matter which character you use you can still put the same animations on them because they read all in the same way so you'll have to excuse my internet connection it's quite slow so here's sporty granny now we can put animations on to her if we slipped the animations tab in mixer mode and a whole bunch of them will come up so if you wanted to put on like these are just still poses if you wanted to make bloody granny run we can select the run animation and it will add it onto the character and you'll see it come up in this window and then if you select another animation it will add that on but it won't remember this running what you need to do to actually embed the running animation into your character for downloading is to select this add to pack now there might be some cases where the animations will have the character actually moving if we select this in place and turn it off you'll see that she's now moving now the problem with this in a game engine is that you want to control your character with the arrow keys or with WASD or control or whatever so you need to be in control of where the character is in the environment if you have an animation where the character is moving within the animation then that causes all sorts of problems when you start to move them around using code so the best thing to do is make sure that you set your animations to occur in place so here's one here I'm going to add that to the pack okay now we want a jumping animation so I want granny to run along and then jump so we'll go up here and type in jump into the search and see if we can find a jumping animation okay so there's a whole lot of different jumping animations that you can see here this one is a jumping on the spot now the problem if you're got a character that's running along and then going into a jump if you select this jumping on the spot the character will need to stop and then jump up come down and then run off again so it doesn't look like a very natural type of movement if you want to run along and jump so something like this here is more appropriate where she's actually running and jumping in a forward motion so if we select that one to add to granny you can see that it's also moving the character so we want to put an in place and this will neatly merge with our running animation so that it will look a bit more seamless than having to stop okay so we're going to add that to the pack as well so we have a running and a jump and finally we're going to add an idle because all characters basically stand around doing nothing for a while and we all need them to actually move when they're just standing still otherwise you lose what's called in animation the breath of life from the character if they're not doing anything for a long time now let's just find a nice idle animation there's a whole bunch of them to pick from happy idle okay let's pick that one just sort of yeah okay that'll do let's add that to the pack right so we've got three animations that we're going to work with in unity running happy idle and jump the next step is to select your add to my assets and then go through the process of downloading so when you get to this page for the cue download and when you select it it will ask you for the download settings and you need to select FBX for unity here and the rest you can leave as they are and and then queue the download and this will take a little while to process and download so I'm just going to download it and put it on the desktop of my computer and I will come back into unity and talk to you there now I'm back in unity and I've opened up a new 3d project and I've already brought in the sporty granny folder that comes from miksa mo okay so all I've done is put the sporty granny folder on to the desktop so that's everything that comes from miksa mo and drag it in and drop it in here which I won't do again but inside that folder there's a whole heap of things first of all there's the sporty granny model which is this one here with the little play button next to it and then you've got these three blue cubes now the name on them is too long to see in this preview here but if you select them you can see which ones they are here this is the happy idle animation this is the jump animation and then this is the running in place okay so I don't know why I got that error there that's not related to granny so if we drag granny into the scene you can see here and I'm just going to zoom in a little bit now I've got my frame turned on I'm going to turn that off see it a bit better right now the camera is currently behind her so that's not a very good view to see the animation so I'll just select the camera and go game object aligned with view to change the view of sporty granny now if we run at this point nothing is going to happen okay so we've got the model in the scene but there's no animations added on to it there's also no physics in there at all so she's not going to just fall out of the air and if you have a look at the model of sporty groaning over here we can see that there's a transform it as an animator which I'll get to in a moment and then there's a whole bunch of meshes but there's no Collider or rigidbody on there so of course it's not going to work in the physics system which we don't want at this point because we're concentrating on the animation right so what we're interested in now is to get these animations onto our model and we do this with an animator controller which is this bit here which says none at the moment so we're going to create this new one and we do that in the project go create and you want to look for animator controller here and you'll get a new one pop-up so the animator controller is this little square box with a few square things few lines and a play icon in it let's name rename this to granny animator okay and then we want to apply that animator to our model so select sporty granny or whichever model you've got in the hierarchy and find the animator that's attached this will automatically be here if it's not you can add a new component and put the animator on that then it take your new animation and drag and drop it into there so now the animations are set to run on the granny model but there's no animations in there so this is just a controller which is empty if we double click on it it will open it up in here so this is the animator window and this is where we put the animations and then set them to run and set them to trigger and all those types of things so the first one we're going to do is grab hold of this happy idle and drag and drop it into the animator window and what you'll see is that it goes orange orange means that it's the default animation so it's going to run immediately at this point if we press play you'll see that the happy idle animation is applied now notice also in the animator that you get this little blue bar across the bottom of the animation that's playing if I just turn off play hit it again just watch this bar as it progressed is through the animation what you'll notice is that a guess of the end and it stops now more often than not a game character that is standing around will be in its idle pose for quite a long time so you want it to cycle through this so that the character doesn't just stop and freeze so to do that we select the idle animation itself that's back in the project down here go across to the inspector select the animations section and then go down and under this timeline find loop time tick that and tick the loop pose that will make the animation cycle for as long as you need it to and then hit apply okay now if we come back and press play we can watch it here that it will cycle through and it just keep cycling through all right so that's the idle animation now if we grab hold of the jump animation and just bring that in you'll notice that it's not orange and that's because it's not the default and it won't play if we want it to be the first thing that plays on our character we right-click and go set as layer default and if we hit play you'll see the jump animation run now it only runs once it doesn't cycle usually when a character jumps you want them to jump just once and the jump will be controlled by let's say the spacebar so you don't want it to continue looping over and over again you want them to jump and then go back to what they were doing before such as running or standing in setting in place okay so we don't want that to be the default let's go back to the happy idle and set that to our default okay let's um at this time also bring in our running for bringing our running now the running you do want to loop because as you're running along it will play through the animation once but your character might still actually be moving so you want that to cycle so just select it in the project and then go over to inspector and make sure that you loop it as well by ticking these two boxes and then hitting apply what we have in this animator window is called a state transition diagram and we use them a lot in computer science to show the different states of the system and the ways that one state can transition into another now in this case it's being used for all the different animations so it is used in other domains beyond computer science but this is where I learned about it so if we want to transition from idle to say jump we can right-click on the happy idle and emotion and make a transition this then gives us this line that we can snap to one of these other animations okay so let's just click on jump and when this now plays it will play the idol it will then transition and do a jump in place so just watch what happens now with the granny so it plays the idol and then it shoots down here and does jump in place and then it stops because it's got nowhere else to go from here if you wanted it to cycle back you could right click make transition and then click back on the idle so let's just run this and you'll see that it plays this one it cycles down plays that comes back to here plays that and it cycles around okay so that's how you do a transition now if we select this and just delete it and we could also make a transition from jump down to running and it will play this this then this automatically so let's just have a look Idol jump in place running now remember we put a loop on the running so it's going to just loop around it's not going to go anywhere else because there's nowhere for it to transition back to now if you have a sequence of transitions that you want to run then you would do it like this you'd have them just automatically play this animation and then play that animation then play that animation the problem with that with the with your game is that you'll want to control the animations with code based on what the character is doing so if the character is idle and then you press the spacebar you'll want the character to jump in place that means we need to program what's going on with this actual transition to do that we're going to add what's called a trigger to cause this transition to run in order to trigger it so to do that we go up to the top corner where we've got parameters this is in the animator view and we add in a new one so select the plus and we're going to work with a trigger okay so not with these ones here which I use for more complex animation scenarios but the trigger so select the trigger and then we need to give a trigger a name so I'm going to call it is jumping now whenever I create a trigger I always use this sort of camelcase where we've got a lowercase at the beginning and then a J capital J for the jumping part so if it was is walking it would be the W that's the capital the spelling and the capitalization of this here is very important it has to be exactly the same as the way that you've got it in your code now there's a little button here that will show you whether your trigger is on or off we want the trigger to be off because we want code to activate right so we've got an is jumping trigger we then select the transition that we want to control with that over an inspector you can then add the condition now you can see it's already actually in here if you don't have it you can select the plus button and it will then give you the option of adding that and because it's the only transition that we've got sorry the only trigger that we've got let me just get rid of that then it'll come up automatically for you now if we play at this point because that trigger is on here this transition cannot happen until the trigger is activated now to activate it we need to do that with some code so in the project let's go create c-sharp script and let's call it granny controls and then double click on it to open it up in your editor okay just make this window a bit smaller it's quite large and the class will be called after the script name I'm just going to replace all of this with my code so that you can see what's going on okay so the first thing we need to do is we create this static variable at the top which is an animator got call it Adam this will link back and grab hold of the animator controller that we've got attached to granny now to set up the animator to point to granny's animator in the start function when this first runs we just do a get component of the animator to put it in here okay once we've got hold of that animator we can then use it to set the trigger now in this update function you can see I've got this code here if get button down is jump now this will activate on your desktop machine for the spacebar so the spacebar is an automatic jump button and when the jump is activated you'll see we're calling a set trigger is jumping so it's going to set the trigger is jumping notice the spelling here very important that this is the same capitalization is the same in order for that to trigger right so save that and go back to unity we're going to add this code on to granny so I'm going to drag it over and drop it onto the sporty granny character and we just select sporty granny looking inspector to see that it's there all right so now when we press the spacebar it's running it will activate this trigger here which will cause jump in place to happen there's no trigger or anything on this at the moment so it will just go into a jump and then start running so let's just test it press play which is an idle and you can be an idle for ages and then press the spacebar okay and you notice that it triggered down to here now what you might not have known is that I press the spacebar halfway through this idle animation now I'm going to tell you and I press at this time and you can see what happens okay so I'm going to idle once I'm going to press space button now and notice that this idle animation had to finish before it went down to jump which is most annoying because it creates this great big lag which you don't want when you press the spacebar in a game you want your character to jump immediately not wait till it's finished playing this animation so to fix that we go to our transition and over in the inspector there is at the top has exit time button um untick it that will cause this animation to be interrupted and go straight down to jump okay so let's play that now I'm going to idle through once and then jump now okay so see that worked immediately okay now we don't want to jump and then go back to running in this instance because we're only jumping from our idle state so you'd imagine you'd be an idle you jump and then you want to go back to idle so can actually delete this transition and after we've done a jump we just want to automatically go back to there so it can create a new transition and just go back and it doesn't need any triggers on it we just want it to run automatically so let's play idle idle idle jump now and that works and it will continue to work with that code okay so that is transitioning with the triggers now if we want to go to run so we're going to use our arrow keys or WASD keys to move our character around then we're going to want to transition between idle and running so I'll just move these around which will require a little bit more code to be added on to our granny character so go back into your granny controller code and you'll see that I've already added the code that's going to move your character around this code here is what I've added just highlight it for you now it uses the vertical and the horizontal axes which are part built into unity which will in the case of a keyboard translate to the arrow keys and it will also automatically if you've got a game console controller it will automatically translate to that as well so it's quite multifunctional and if it's on a mobile device then your little thumb stick controller will also control this code which is really handy because you're going to rewrite it this code here is actually from the unities scripting website which I always go in and grab to use it to move an object around which will rotate and move forward and backwards and all I do whenever I forget what this code is if I if you type in get access and you go to the manual for that it will give you this little bit of code for driving an object around so basically what it's doing it's got a translation value which is going to move the character forward and backs and rotation which will rotate the object when the character is translated in any direction it translates it based on the z-axis of the character as you can see here so it's always pushing the character forward remember that the forward axis of the character is its z-axis and the rotation will rotate the character so it doesn't matter which way the character is facing it's going to push along z-axis which has the effect of allowing you to turn and move the character forward and back now along with this little bit of code you've also got some variables for speed and rotation speed which are up here as public floats speeds are set to ten and rotation speed set to 100 so you can change that based on how fast or slow you want your character to move right so save that and just go back to here press play now if you're on the keyboard you can use your arrow keys and you'll see that the up and down arrow key is moving the character forward and back and the side arrow keys are going to rotate your character and then the up-and-down will move it in that particular direction what you notice is that granny is remaining idle she probably will still jump there you go but you want at this point when she's moving forward or back to be in a running animation now unlike jumping running is more of a you are running or you aren't running so you've kind of got two states and if you aren't running you're idle so instead of creating a trigger parameter we're going to create a boolean so if is running is true then we want to be running if is running is false then we want to go back to idle so we're going to add now a bool and we'll call that is running okay don't tick the box here and then we're going to create the transitions between happy idle and running so we're going to make a transition to running and then we're going to make a transition that goes back as well now from happy to idle we're going to add in is running and in the conditions you'll notice now because we've got a boolean value we can set whether it's true or false so if is running becomes true then we're going to transition from idle to running and back the other direction we're going to transition from when is running becomes false so it will go back the other way okay so in the code what that means is we have to test that when the character is moving forward or backwards and we want the character to be in a running animation which means we want to set the boolean value for our is running trigger and we can do that in our update function how do we know if the character is moving this translation value here is the amount of movement forward and back that we're adding onto our character with this translate function so if we test for is I'm sorry if translation is zero or not we know that the character is moving so I'm just put this code down here for us and it's going to say if translation is not equal to zero then we want to set running to true otherwise running will become false so we can control this that particular I'm going to call it a trigger even though it's a boolean value still triggering that animation on and off between the idle and the running states now the other thing you want to do save that and go back to here is you want to make sure that you've interrupted the animations again so if you select that and then select the other one and turn off this has exit time and then we press play you'll notice that she's now running and when you stop she stops running as well if I come back and the jump is still in there now the jump currently requires that you go back up to idle and then come down to the jump animation so you may want to add a transition that goes between here and here as well to ensure that the character will jump as she's running rather than having to go back to idle and down to jump in place to do this we just create transitions between our running state and our jump state and set the trigger here to our is jumping which we've got which with the spacebar is going to trigger and then we come back with a make transition to running but in order to ensure that we don't go back up to the happy idle from the running we can put our boolean of is running here is true on to the transition back to jump so if you're running along and you jump you come back to running in place as well so if we just now run this if we jump in place we will come back to the idle if we are running and jumping and you can't see but it does switch back here if you watch that really quickly alright so we need to do is to now rotate our model around it and put the camera and attach it to our character so we can see what's going on because there's not really a lot of space to watch how she's reacting to this so let's just go back to the scene and I'm just going to rotate the model around like this so let's say you want to follow your character from about let's say there are grab your camera once you've aligned here select the camera and then go game object align with view and that will set the view of the camera to where you want it in the scene now grab the camera and at-at ship to sporty granny so just drop it onto your model now wherever your model goes your camera will follow and you can see that it doesn't appear as though the model is moving but it actually is and you've seen that in the scene okay so if we now press play and we're running along and we press the spacebar you'll notice that there's a bit of a lag going on there that we need to fix to make it a little bit more seamless okay so the first thing to do for that to go back to our animator and make sure that when we come back from our jumping to running that we not have this exit time occurring and the same thing when we're going from running to jumping is that that exit time isn't going to be on right so now when we run it and we're running along and we're jumping you can see that that jumping animation is getting interrupted which is something that you might not want because it doesn't keep it very fluid does it so although you might want to interrupt the jumping straightaway you don't want to interrupt coming back from there so we can turn that back on that has exit time and run it so we use the up-arrow key and we're in our running if we hit the spacebar watch what happens it's going nicely into the jump but then at the end of the jump there's this sort of standing still momentarily and if you watch what's happening over in the animator view you can see that although we've got is running set to true it's still flipping back up to that idle state so it's going momentarily through idle and then back down to running causing a set jittery sort of movement so we need to then stop this from triggering back to here and you'll notice there's no conditions on there so let's add another boolean value for idle and we'll call this is idle and then we're going to add that to this transition here going back from jump to happy so we're not going to go back in that direction unless we're in the idle State so when idle is true we'll be allowed to go from the jump in place back to the happy idle so at this point we're not setting that at all this will cause a few issues so we're running we press the spacebar we jump and we go back into running again okay there's a little bit of a lag between the two animations which we can fix up in a moment but we're not going back up into the happy idle which is good now if we stop running at this point and we hit spacebar we will jump but notice that although we played the idol that trigger thing didn't happen to cause us to go back to this state because is idle has not been set to true that means you need to go into the code and correct the problem so in here we've got set bool is running to true if we're moving around if we are moving around then we have to assume that we're not idle so we need to set is idle at this point to false and otherwise if we aren't translating then we must be idle and we set that to true so we're kind of setting is running and is idle to the opposites of each other in here okay so then we save that and go back to granny and run we should be able to jump and go back into idle and we should be able to run and jump and go back into the running okay so just to remove that little laggy stand thing that happens at the end from this transition to the other you could use what's called a blend tree but in this case we don't need one so although if you just select this transition between jump and running and come back over here inspector we've got this has exit time ticked to ensure that the jump animation runs for the whole extent of it so it doesn't just sort of stop and go straight back into run immediately and have a little bouncy jump you can go into the settings of the transition and set the interruptions source so that at the very end of the jump animation which is its exit time you can cause a little blend to occur between the jump and the next state so if we select the next date is our interruption source and then we run it again we'll find that the little lag has now gone because it's a really nice blend of the next state and the jumping animation alright so that's the end of this tutorial I will do a tutorial that shows you how to use the blend States after this one but in this case we don't need it so if you find that you can use these settings here for the interruptions between the states and it will do a little blend for you without going into the complexity of blend trees then this is definitely the way to go all right so I hope that helped you learn a bit about Maximo and the mecanim system in unity of course is just the basics and there's just so much more to learn and hopefully I'll get some time to put some more tutorials up thanks for watching and I'll talk to you in the next tutorial
Info
Channel: Holistic3d
Views: 234,123
Rating: undefined out of 5
Keywords: Unity 5, unity, tutorial, beginner, mixamo, mechanim, c#, blending, triggers, character, moving, animation, animating, education, basics, basic
Id: BEIaakl9vJE
Channel Id: undefined
Length: 37min 2sec (2222 seconds)
Published: Fri Feb 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.