Main

Road To GameDev Episode 4: How to Movement Script and Animations in Unity

Hey guys, unfortunately only 720p again... Quick Time Player recorded my Screen badly. However, here I show you how I added an easy Movement Script and Animations for a 3D Character. The Videos I watched for this Progress are: For Animations https://www.youtube.com/watch?v=5mlwvbu1fxQ&list=LL&index=8 For Movement: https://www.youtube.com/watch?v=CieCJ2mNTXE&list=LL&index=9 How I changed the Model to humanoid: https://www.youtube.com/watch?v=Cxn24UKtw4A&list=LL&index=1

SertoriusGD

3 weeks ago

hello and welcome back to my channel Z J here and we made progress as I said in the last episode I will redo everything and I did so we have a new project a new code and we have animations and everything works I forgot to press record that means I will show you everything like live now and explain how and what I did basically this is going to be a how to make a basic 3D character with movement and animations I guess yeah why not keep in mind all the animations and character I have right now are
not final those are just placeholder you can say to for me to understand everything I need to make the animations and the character on blender all right as always without further Ado let's jump into unity all right we are in unity and what I did I added a ground in again of course to walk on it has a box collider and a ground tag that will be important when I show you the code and of course we have our player what I added to him is is I downloaded this player from the asset store it was free so
I just took it and put it in what he has a rigid body for the physics a capsule collider it has one capsule collider right now but I think when I have my final player every part of the body will have a separate collider like the rag doll over here I just try to play with the rectal but right now for the purpose of learning we have only one capsule collider for the character I have an animator you see that on the ground this is for the animations I put the animations in there in the controller an
d of course the humanoid Avatar in there and finally of course he has the script component I think I will show you the player how it moves first and then we can jump into the code so he changes from the T POs to the idle animation and as you saw when I move him he runs on the ground on the bottom you see the animations that is loaded right now so if I don't move it's the idle one if I move it jumps to the Sprint one and when I jump it goes to the jump animation as well as when I'm running and I
can run into the r doll or walk through it jump on it jump on it beat him up beat him up and then yeah that is a problem I had as well when I jump into the rec doll he starts spinning you spin me right now mayy right now all right never mind so I'd say 90% works and 10% spins all right let me show you my code I refected it and I made it new firstly of course we have our Global variables we need speed the inputs the jump the rigid body is important actually I'm going to move it down here oops I t
hink that looks better right with the animator so the both components the character has are here and those are the variables I will change for the movement in a start we have to declare the components or the variables and what I did is the update function only has the functions yeah the functions are called it is not implemented in here just called here it is easier for me to understand when I want to change something to find the right place so I have the handle movement which is for walking and
rotate the character and we have the jump and of course a collision enter but I don't have to um call the function this was the old one it's just everything in the update I didn't like it all right the handle movement as always we have the inputs for the horizontal and vertical we can just add horizontal and vertical in here because Unity actually save the option or the setting if you use W ASD all the arrow pads Keys it it but everything works it's just saved in the horizontal and vertical tha
t is really nice then we make a new Vector of course with a new excesses the first if statement is if the move direction is not zero so it moves from zero to another place firstly we set the animator to one I will show you why oh no I show you right now so because every animation has a um condition on the Running Animation the condition is that speed is greater than 01 and we make that with this line we say if the character moves the speed variable go goes to one so it's greater than 0.01 so the
animation changes to the Running Animation right and uh these two codes uh lines are for rotating the character so he just look looks where he runs right so he rotates into the walking or running Direction makes sense right and if the character does not move we set the float to zero again because the condition for the idle animation is that the speed is less than 01 so no moving means idle animation moving means Sprint animation and this line is just for um translating everything and time Delta
time is important one here so it always has the same speed when you walk and it does not matter what your frame rate is right now I hope I can explain it good enough I'm not the best explainer but let's jump jump into the next function you see what I did there so the handle jump we start with the if statement and we say if getting get button down jump so it is the space bar for me and when the character is on ground let me just show you this the character has an onr variable here yeah oops I ju
st edited out so and it is checked that means the character touches the ground and he can jump I will show you that in a second so now you see on ground is checked when I jump and he's in the air it is not checked anymore so he can jump while he's in the air right this line is just him to tell him to jump and here it is we change on ground to fals so if you press the jump button on ground goes to fults that we don't jump while we in the air put your hands up in the air all right never mind and t
he next statement if he's not on the ground we set the Boolean jump to True first first I had trigger but it didn't work so I changed it to a Boolean as I said when the on ground is not checked because you press the space bar the condition here jump goes to true so the jump animation starts and it does that when he is in the idle and when he is running so he can jump while standing and while running and if he is touching the floor the jump Boolean is false so the animation does not start no now
when you're jumping on ground is on false but when he touches the ground again it should go back to true if not he only can jump once in a game and that is not what we want so we add this on Collision enter function just really simple this one says if the character which has this script touches the collider from another thing which has the ground tag and in our case it's the ground which has the ground tag we tell him to change on ground to True again that means in the air on ground is false but
as soon as it touches the ground it's true again so we can jump again and that basically is the code for the movement and jumping from the character way easier than what I had before let me guys show you the animations again those are again animations I just downloaded from from the asset store for free on Entry he instantly goes to the idle animotion animotion animation no conditions whatsoever the first condition we have is speed greater than 0.01 what I told you if it's more bigger than 0.01
he goes to sprinting another condition if jump is true he jumps and from jumping back to idle or running there are two conditions jump and speed so when jump is false he goes back to either one of this animations idle or running but the other condition is if the speed is less than 001 it goes to idle if it's bigger or greater than 01 it goes to sprinting so he just when we run and jump and keep running he keeps running after landing on the ground the parameters are made over here speed and jump
and like I showed you I Chang them in the code I think I can delete this old one that's B basically it that is how to how I made the character and how to let him jump walk with animations all right guys that is it from this episode I showed you everything I done so we actually have progress and right progress what I want to do in the next episode is at punching and a health bar because every character in the game will have a health barff so he gets knocked out when he's on zero and is like para
lyzed for two or 3 seconds and then respawns like in rocket league if you get hit with from someone with a boost you are like down for 5 seconds three seconds somewhere like that I will see how it looks we also need the ball and and picking something up and throwing I hope you like this life explanation I hope I could explain it good enough for you guys I actually liked it more because I don't have a lot to edit I save so much time with this so I will basically just code and do everything quietl
y and then explain afterwards everything live all right I hope you guys like this episode I hope I could help you guys so we see us in the next episode take care peace

Comments

@mert1296

Trust the Progress!🎉🎉