CHARACTER CONTROLLER WITH ANIMATIONS - WALK AND ATTACK - Unity 3D - Quick And Easy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and girls and welcome back to another video in this video I'm going to be showing you how you can make your own character that can move and attack with animations I've been playing around with this for the past week and I really haven't found any good video or or any forum that explained this well except this one video so if you have the same problem hopefully this video helps you first thing you want to do is go and download this to an RTS units demo or you can just use your own models and animations so download it and import it into your scene first thing you want to do is go to toon RTS here go to models and drag your toon RTS into the scene I'm gonna set my position to 0 0 point 5 and 0 now what I can do I can take the camera move it up a bit and move it backwards a bit more up and rotate it a bit and then what I can do I can drag the camera to the toon RTS demoknight this is just a quick way of making the camera follow the player you can also do this through script but we're gonna keep it like this just for simplicity what you want to do now is go to tune RTS in your project view go to the inspector and set the animation type to humanoid and set the avatar definition to create from this model click apply and now go to your animations and go through every select all the animations actually an animation type set animation type to humanoid and copy from other avid avatar click apply go back to your model and here you see this Avatar if you click this arrow here it's gonna open up a few things and the last thing is your avatar so select all the animations and drag your avatar to the source here next we have to create the animator controller so right click create animated controller we're gonna call this night mm controller then take that and double click it and it's gonna open up a animator I'm just gonna drag it here so you have a nice view you can move around in the animator by holding your middle Mouse and you can zoom out with the scroll scroll wheel select the night in the scene and if you have a look at animator you can see a controller here so drag your controller up here also we want to add a character controller to the to the character you can see that the character controller is not placed properly so we're gonna Center it on Y 21.5 that might be a too much so 1 and we're gonna set the height to 1.5 actually this is a bit too high so go 0.5 on the why not write 0.7 maybe that seemed about right also I'm gonna add a rigidbody and set it to be kinematic go to animations and find your idle animation and just drag it into the animator and it's gonna be orange now which means it's the default default animation so if you click play you can see that now your character is idling but you can probably see that the character is rotating a bit which we we don't want to do so go back to your animation put the animation tab I set loop time which should already be set which ones check is root transform rotation and I'm also gonna check all of these because I don't want to move the move the player on any axis you can see that it's not rotating anymore and it looks much looks much nicer we want to add the run animation so you can either use charge animation or walk animation I'm gonna use the walk animation then you're gonna right click to your idle animation go make transition drag it to the walk and then do the same thing but new verse go to parameters up here and create a integer call it condition and go to this transition so when you're transitioning from idle to walk you want to you want to check if the condition equals to 1 so if the condition is 1 then we're gonna start walking if the condition is not equal to 1 then I want to go back to idle so if we click play we're idling but if we change the condition to 1 we're gonna start walking now you can see that if I change it to 1 it doesn't start the animation immediately that's because we haven't unchecked has exit time what that means that it's gonna run the next animation only when this one ends so uncheck has X time and do the same thing on the other transition if we click play now change to 1 and it starts automatically very good but you can again see that our character is rotating a bit we can fix that by going to our animation and setting all these checking all these again click play apply it and then play again you can see that it's not rotating anymore and it looks very nice now we're gonna control this condition through script and then we're going to be able to change the condition to 1 1 we're pressing that button so create a new C sharp script I'm gonna call this night controller first thing we want to do is create a few variables we're gonna need a float for speed which is going to be for a float for rotation speed I'm gonna set to 80 we're gonna need a float for gravity what I said back to 8 you'll also need the vector three for move direction I'm going to set that to vector 3.0 that is same as saying vector 3 0 0 0 we're also going to need a variable for our character controller and animator so create a character controller controller ok the animator and now we're going to use void start to set those variables so controller will be equal to our component character controller and our Adam would be equal to get component animator if you're new to unity and scripting what we just did we just referenced our character controller on our player and a animator when I on our player now we're going to use void update and we're going to do a few things first we want to check if controller is grounded so if we're standing on the ground if we're not falling or flying then we want to start moving so we want to check if input that get key key code W so if we're holding or pressing the W key we want to move so we want to set our move Direction be equal to a new vector3 that is going to be 0 0 and 1 so we want to only move on the z axis also gonna set our move Direction x equal our speed so we're gonna take this one and then multiply it by speed this is the same as saying equals move Direction times speed it's the same thing as this now we're going to say move Direction dot y so that is the up and down axis and we're gonna set that to be equal minus equals gravity times time Delta time so every frame we're gonna be moving our player on the y axis by 8 so we're just going to be lowering him to the ground the last thing we're gonna set our controller that move and we're gonna move on move the reaction times time.deltatime go ahead and take our script and drag it onto our player or our night and see if I play press W we start moving forwards but the problem is we don't stop moving how we can fix that is by saying here if input that get key up so when we stop pressing the W key decode a W then we want to set our new vector once set move Direction to be equal to vector 3 that 0 or just 0 0 0 so we don't want to move just move forward if I stop pulling down we stop moving but the problem is we can't rotate yet just above where we're setting our gravity I'm gonna add a few more lines I actually forgot to make one variable which is going to be float rod for our rotation which is going to be 0 when we start the game so here we want to set our rotation plus equals input baguette axis horizontal so those are the a and D keys on our keyboard and also left and right arrow we're going to multiply that by a rotation speed and also time that delta time and one more thing we're gonna say transform so that's the transform on our character this one Euler angles is I think how you pronounce that I'm not sure and that's going to be equal to new back 2 3 there's going to be 0 irritation and 0 if I click play you can see if I press a press the a key I move left I mean I rotate left if I press the D key I wrote it right but the problem is no matter our rotation if we move forward we're always going to be moving in the same direction you can fix that by going here where we're setting our move direction and add one more line say move Direction equals transform that transform direction so if you if you read here that what that does it etcetera sets our transform from local space to world space transform direction and we want to transform move direction you can see that now we can rotate and we move in that direction pretty nice but we're still not using our animations we're gonna do that now we're gonna do that by using this condition and setting setting it to one when we want to run the animation and set again to zero when we want to stop the animation so go here and when we're when we're pressing the W key we want to go and set the integer condition to one so that this line is same as going here and setting this to one and once we stop pressing the the key we want to go set integer condition to zero which means we're going to go back to idle animation if we click play now you can see if I press the W key we start the animation if I stop holding it it stops the animation next thing we want to add is the attack animation and this is gonna be a bit more complicated so you want to delete these two transitions and you want to add a new empty one I'm gonna call this one transition and then I'm gonna again create a few transitions and also I'm gonna add my attack animation so just drag it in go through all the transitions and just uncheck has exit time when we're transitioning from idle to transition we want to check if condition is not equal to zero so if if we're not idling what we're doing something else and then we want to idle when condition equals zero so the condition is not zero it's going to go to transition and then it's gonna check do we want to walk or do you want to attack so if you want to walk we're gonna set the condition to be equal to one and if want to attack we're gonna set our condition to be equal to two if you want to stop walking we're gonna see if condition is not equal to one so if we're not walking with either idling or attacking and do the same thing for the attack this looks good once you have this set up all you have to do is just add a few lines of script and you're good you're good to go if we set the condition to two we play the attack animation if it's one we start walking if it's zero we're idle and if you play the attack animation you can see that it's doing the rotating thing again so go to the animation and just check these three and apply now you can see that it doesn't do that anymore and it looks good I want to create two more parameters both of them are going to be boolean so those are the variables that are either true or false we're gonna make one for running I'm gonna make one for attacking here we're setting the intake the condition to one we also want to set the boolean running so once said running to be equal to true and once you stop running we want to set the running to be false that looks good but for just making your skip look nice we're just gonna copy this and delete it and then we're gonna create a new void call it movement and just then just paste this here this is just so our update map that doesn't look ugly and it's easy to read we want to create a void for attacking so I function for attacking and also a function for get input so first we want to in the get input we want to check if the controller is grounded because we don't want to attack if the controller is not grounded if he is grounded then we want to check if we're pressing the left mouse button so get mouse button down so when we press it and 0 which means the left mouse button so when we do that we want to call the attacking function in the attacking function we're gonna set our that are integer in the condition to two which means we're gonna play the animation just go up to move to avoid update and call the get input function you see if you press the left mouse button we attack but the problem we have now is if we're walking we can't attack we want to fix that so what we're checking for the left mouse button we will also want to check for if if we're running so an animator get boolean running so if the if running is true then we want to stop running we said running to false and then we want to after that we want to else if want to check if we're not running which we're definitely not because we just said it to be false so if running is false then we want to call the attack function and also here when we're setting running to false you can also set our integer Aki condition to be zero okay so what we want to do is go ahead and create a enumerator attack routine and here if you want to wait for a few seconds you type yield return new wait for seconds and then it happened how many seconds I'm gonna say one second so in attacking what we're gonna do we're gonna say start co-routine attack routine and once we wait one second I want to do is we want to set the integer actually are Condit condition to be zero also before we attack we want to set our boolean attacking to be true and once we end we want to set it to be false looks I have an error I forgot some parentheses here so we move we attack still doesn't work okay maybe if we do this in the ienumerator function it will work okay so go back to our movement up here and we're gonna say if we're holding down the W button we're gonna check if our if we're attacking so get bull attacking it's true so for attack if we're attacking if we're attacking we just want to return and then else if Oh so if we're not attacking then we want to do the actual thing so then we want to know so if we're not attacking we want to move if we're attacking then we just return okay so if we remove this else here then it might work oh yeah that was it so if we're moving we press left mouse stops attacks and then lets us move again so that is it guys for this video I know it was a very long video it's it was very complicated but I just wanted to like make a tutorial on it so if somebody's having this problem same as I did a week ago you know hopefully helps them out so if you enjoyed be sure to leave a like comment down below if you want to see any specific tutorial or video if you're having some problems also comment down below and also subscribe it really helps me out and hopefully I'm gonna see you next time bye bye [Music]
Info
Channel: Single Sapling Games
Views: 339,133
Rating: undefined out of 5
Keywords: script, make, game, for, free, 2018, development, dev, challenge, unity, blender, paint.net, paintnet, gimp, mono, develop, hacknplan, hack, plan, program, tutorial, guide, new, hd, twitter, youtube, itchio, itch.io, developer, indie, design, art, animations, animation, animator, controller, player, model, character, enemy, animating, quick, easy, fast, in place, in, place, 101, basic, animate, c#, best, how, to, how to, unity animation tutorial, lesson, walk, attack, run, strafe, learn
Id: ReauId6jFFI
Channel Id: undefined
Length: 25min 15sec (1515 seconds)
Published: Tue May 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.