Project Description

Subscribe

Title: Mecanim Tips: Basic Walk Scripting
Platform: Unity Tutorial
Length: 10:28
Difficulty: Intermediate

Animations: https://www.assetstore.unity3d.com/en/#!/content/5330
In this Unity tutorial I’ll go over the very basics of scripting a walk animation in Unity. Due to the number of questions I’ve received in regards to scripting animations with Mecanim I decided to create a simple tutorial that goes over the simplest of scripting for Mecanim.

hello everyone this unity tutorial we’re going over how to script the simple walk script in mechanism going to be using a script to control a parameter is going to have our character walking so if I hit play our character is currently in a idle state if I hit the W keys gonna start walking forward if I let go he’s gonna stop this is a very simple script we’re gonna be going over a lot of you have had some issues scripting with mechanism so what kind of break it down to very simple walk script so that’s weird recovering today so let’s get started so you want to go and download the project files help you providing a link to it in the description of the one thing I won’t be providing is the wrong mocap data package that is the only asset from the assets story it contains all the animations we’re going to using for our characters so I cannot provide that you’ll have to download that in imported into a project the character however is already raked up ready to go is already for mechanism everything’s already set up so we can just focus on the animations and the scripting so if we click on our mail rig right here in the hierarchy we can see our animator component is already filled in with our controller right here the controller comes from the project panel right here and it’s called Adam if I double-click to open it up you can see it’s basically empty it’s ready to go so we can add some animations in here now if we want to add some animations we can open up the wrong mocap data folder and I’m going to create a tidal state first so i want to go and click on a idle animation open up the animation folder open up the idol folder and then in here we’ll look for idle neutral 1 i’m going to click on that and then you want to open it up we’re using the idol neutral one this is the actual animation clip itself alright let me go back to my animator window make sure you have it open and if you don’t have it open just go back to your adam animation controller or your animator controller double click on it and it should open up this window let’s go back to the animations in here so you want to go and select the actual idol neutral one animation clip this is the clip this little blue cube is the prefab so just bring in the clip just drag it in there we go and you can rename this whatever what I’m gonna do that in the inspector panel so I’m just gonna go to name it I don’t delete all that I’m going to uncheck foot ik and now if we have to take a look at our mail game rigged he has the animator controller already assigned to him so this should play automatically it looks like the name didn’t stick I’ve got to hit enter alright we’re going to this one more time there we go alright so we hit play and our character is in the idle state if I going to drag down my animator window we can see this in real time so the idol say is currently playing it’s a very long animation clip so it’s going to continue playing all the way up to the very end it’s going to look back to the beginning so we want to create a transition from going from idle to another animation clip and we’re using a walk state so it’s going to hit play again i’m going to talk this back over here all right and now what we want to do let’s go back to our animator window and we’re gonna be using a walk state in here so i’m going to go ahead and move this down here let me go ahead and close up some of these folders i’m going to the walking folder and we’re using a animation called walk forward alright so just like before we’re just going to drag in the animation clip itself drag it into the window and there we go I’m going to check on foot ik and now we want to create a transition going from idle to walk forward so let’s just right-click on the idol see make transition and click on walk forward right so if you do that correctly you should have a transition with the arrow pointing towards the walk forward state you can go to click on this and you can see over here in the inspector panel we have our Idol transition over here i’m going to minimize this a bit so we have the idol and we should clip here and then we had the walk and we should click down here so by default the condition is going to be set to exit time with the value of one that means it’s going to play the entire animation clip of our idle state and then finally transition into the walk forward state now we don’t want that we want to be able to control that transition we want to be able to tell it hey if i press a certain key i want to go into the walk animation already alright so we can do is use a pro so right here which is parameters want to go and click on the plus icon and then you want to go and select float is going to name this walk with the lowercase W it’s important to understand we’re gonna be using this in our script so needs to be written exactly the same way in our script so walk with the lowercase W now if we go back to our conditions for our transition here we can select walk so for a walk i want to be greater than . one this is a float values are looking for a flute value in here so whenever we use our W key which is going to be the walking button that we’re gonna be using where to look to see if there’s a greater than value . one so whenever you click that button is going to go greater than . one meaning we want to transition from idle to walk forward so we need to do now is create a script that controls that parameter going to close some these up in here so it’s going to right-click in our project panel create csharp grip and i’ll just call this walk let’s call it walking just so there’s no confusion there double-click to open it up alright let me going to move this over bits right at the very top I want to have a couple of variables and the first one is going to be a private variable so private animator we’re gonna be looking for a animator controller sorts of type animator and i’ll just call it NM then I want to have a another private variable so private float will just call overt and is going to be short for vertical so in the start function up here and go and say annum equals getcomponent animator now since this is going to be applied to actual character that has the animator component this will work perfectly fine if you’re not going to be applying it to a character that has the animator component attached to it then you specify where that animator component is at so in the update function we’re going to go ahead and set our verte variable service invert equals and we say input.getaxis and that is going to be vertical now we’re getting the vertical axis here from the actual input manager if you somehow have changes you want to make sure that matches up right so by default the axis is going to be vertical right really exactly as such as the uppercase v and so we’re continually going to update this variable with this axis so whenever we’re holding down the W key that vertical axis is going to change in value and that’s going to be a float value so next wednesday annum that’s that float all right want to set that float for our parameter in our animator window and i’m writing walk with a quotation mark so walk lowercase W remember were spelling it exactly the same way as we did with our programmer so that is the parameter name and then we have a comma so you can tell right here has this little info box says anti-d which is the parameter name then we have a float value since since this is a float want to set that float value going to equal to whatever verte is currently set to you right so for holding the W key the inverter is obviously going to be higher than . one which in turn is going to set that transition to start working so that’s all we need for the script i’m going to go ahead and save this and minimizes alright so now we need to apply our script to our character going to select your character and drag overly c-sharp script we go there’s nothing there that we need to adjust so it’s good to go now if we are going to hit play hold on my W key he starts walking forward now currently if I let go of my W Keys going to continue walking forward because there’s nothing saying hey I want you to stop walking when you let go so we need to do is go back to the animator and we need to create a transition going back to idle so right-click on walk forward make transition go back to idle and click on that transition so in here we have a couple animation clips is going to clean that up a bit going to move the transition right here this transition . over could move this out of the way we go you only want to have one walk forward animation clip here you don’t want them overlapping alright so once again we’re gonna have the same condition or go for a walk this time I want to go and say less than alright so if I’m less than $MONEY . one and where to go back to the idle state so hopefully that makes sense we’re going from idle to walk forward we’re seeing greater than . one so we’re actually looking for a button input and it’s set to greater than . one once we let go then that value is going to drop down below . one is going to drop down to zero and then we want to go back into our idle animation state so now if we hit play although my W key starts walking if I let go it stops right it’s going to bring in our animator window once again down here and we can see this in real time once again alright so we’re in the idle state hold on my W key you can see that walk forward is currently active and it’s going to continue looping until I let go if I let go he goes back into the idle state you can see it went back down and we’re currently in the idle state so that is a quick and easy way of scripting mechanism inputs for our animation transitions so i hope you guys enjoyed this tutorial thanks for watching