‫Welcome back.
‫In this video, we are going to make our character move.
‫And it's going to be a very simple movement going towards the left and towards the right, left and
‫right.
‫So whenever we click or press a button, it should go and left, left and right.
‫So let's go ahead and build that.
‫So we start off by going to our character here.
‫Let's click on the character and let's add a component to it.
‫And I'm going to use scripts, I'm going to create a new script and I'm going to call that one char
‫controller, which is character controller, and let's open that one up.
‫So what I'm going to use is the awake method instead of the start method, and I will need the rigid
‫body because I want to access the movement components, pretty much so rigid body, I'm going to call
‫it RB and then I want to have.
‫A Boolean, which tells me whether he's walking right or left.
‫So if we have a look at the player again, currently he's walking, right?
‫And then if he goes there, he's walking left.
‫And what that means walking right means rotation of 45 and walking left means rotation of -45.
‫So these are the two values that are relevant for me.
‫All right.
‫So let's go back to the script.
‫Well, the Boolean is walking, right?
‫And I'm going to set that up as being true.
‫Then within the awake method, I'm just going to initialize my rigid body.
‫So I'm going to get the component rigid body from our player because the controller is assigned to our
‫player or our character, and that's why this is going to work.
‫Then I'm going to use the fixed update in order to move the player forward.
‫So let's do that.
‫Ah, let's create the fixed update method.
‫And in here I just say set the transform position of the player as the transform dot position plus transform
‫that forward times two times time delta time, dot delta time.
‫So that does it moves the player by a certain amount forward depending on how much time has passed and
‫in fixed update that should always be the same time.
‫So we are moving our player step by step and it's going to be moved forward.
‫What does forward mean?
‫Well, forward means based on the direction that he's currently looking at, he's going to go forward.
‫So if he is, let's say if he's at 45 degrees, then he's just going to go forward here to the right.
‫And if he if he is at -45 degrees, then he's going to go forward to the left.
‫So no matter which angle we choose for him, he's always going to go to the right direction.
‫Next, we can go to our update method.
‫And in here I'm just going to check for the input.
‫So if the input thought get key down or get key actually down, then key code dot, let's just use space.
‫So whenever we press base, we want to change the direction of the player.
‫So let's create a new method which handles that, and I'm going to call it a private void switch.
‫So what that does is it's going to change the variable walking right to not walking, right.
‫So that means if I'm walking right was true, it's going to be false now.
‫And if it was false, then it's going to be true now.
‫So that's just what we say here.
‫And now we check if walking right is true, then we transform the rotation.
‫Then we change the transform rotation value to criterion dot ula and here we can enter the degrees so
‫we can enter zero, 45 and zero.
‫That means that I'm just going to change the direction of to where he's walking by 45 degrees.
‫So pretty much I'm going to change the direct the transform of the player itself by 45 degrees.
‫So that happens if he walks right.
‫And if he walks left, we will need to change the transform rotation value to criterion EULAR zero -45
‫zero.
‫So as I've just shown you in unity, that's what we're going to do.
‫So we're going to change the Y to 45 if he's walking right, and -45 as he's walking left.
‫So now that we have created this script.
‫Let's go ahead and save it.
‫Let's go back to Unity.
‫Let's save our scene.
‫And by the way, we should maybe clean up as well here.
‫Let's create a new folder.
‫I'm going to call it script and let's drag our chart controller into the scripts folder.
‫And before we test it, of course, we need to call our switch method within the update method.
‫So whenever somebody press a space, we will go or we will change direction.
‫So let's check that out.
‫Our player is moving and I'm turning and turning and boom, he's falling off.
‫So let's do that again.
‫Let's move and then forget to turn and he's falling off.
‫All right, so now we have the basic movement of our game.
‫Of course, it doesn't look very well because he's not animating.
‫He is just holding his hands to the right and left, which looks a little weird.
‫But we're going to change that within the next videos.
‫So first of all, what is really important, what we will need to do is to change our camera because
‫now it's not following our player.
‫So our player is simply running out of our sight when we can't see him.
‫So let's do that in the next video.