Welcome back.
In this video, you will learn the basics of moving a object within our game, and that one will be
moved by you, the player.
So you will see how to use user input and how to use basic physics.
But of course, we'll have a closer look at physics within the next videos.
So let's go ahead in our game.
Let's delete our cube.
So let's delete that.
What I want to use is I want to use a 3D object, which is a sphere, and I want to have a 3D object
which is a plane.
So I do right click within my hierarchy view and then I go to 3D object and use plane.
As you can see, my plane has the transform position of 000 for the X, Y and z value.
This transform component, as I told you in one of the earlier videos, is always there for each and
every single object that you have within your game and you can change it around here.
For example, you could change the X position to move it back and forth.
You could change the Y position to move it up and down.
As you can see, that could be very relevant for you.
And as you also can see, we have the sun here which has a specific direction.
As you can see, the sunlight comes from the top left to the bottom right.
And that's why you get this shadow here.
So all of that is already taken care of.
You don't need to program all of that.
It's already there, which is great.
Our play now is at a position that I don't like to it to be, so I can always reset it.
So you can click on to that little button here on the top right side and choose reset.
By that you will reset it to the standard values of 000 for the position and for the rotation, and
at the same time, the scale will go to one, one, one.
All right.
So now let's move our sphere, however, so that it's not inside of our ground.
We can easily change that by moving the Y value up.
And you can either do that by manually entering a value here or you could drag from the y.
So hover over y with your mouse and then you can move it to the left and right and it will change it.
So now you could try to find the right position for your ball to be at and that one looks good.
So 0.37 But what you also can do, you can choose the transform tools here at the top left, for example,
the cross in order to move your object right in here.
All right.
So now what I don't like is the fact that all of them are white or all of my two objects are white,
which means our sphere and our plane.
And we can easily change that by creating an.
A new material.
So let's create a material and call that.
Ground and now we give the ground a different color.
So let's choose, for example, a green and now we can drag that material into our game.
And as you can see, our plane now is completely green and we can duplicate our ground, for example.
So let's do that and we can call it player.
And there we can, for example, use a red color.
So let's take this reddish color here and drag that into our player.
As you can see, if I drag it or hover it over while pressing the left button on the mouse, I can hover
it over my different game objects and for example, over my plane.
And then you can see the plane turns red or over my player, and then my player turns red.
If that doesn't work for you, you can also do that over here on the left hand side, on your hierarchy
view.
You could drag that onto your sphere and then the sphere will turn red as well.
Now, let's have a quick look at our sphere.
As you can see now, it has gotten a red color here, which means the component has gotten a color of
red, which is our red material.
Now we have our playing field.
We have our player or our sphere, and I'm going to call that player.
So that one will be my player and the plane will be my on the ground.
Now, in order to make our player move, we need to create a script for that.
And we have created a script already.
I call it test, but I don't like that script, so I'm going to delete it.
And as you can see, if you delete a script, this action cannot be undone.
So be very careful when deleting assets from your project.
So I'm going to delete that one now.
All right, so now let's create a new script, which I'm going to call player movement.
Now let's open that up.
And usually Visual Studio should come up or whichever editor you have set as your standard editor.
So next, what we'll need to do is to create two variables and use methods from here.
It's no problem if you don't understand everything that's going on here.
We will cover that in one of the later videos.
In the next chapter, I will go into detail what this using means, what this public means, this class
means, and so on.
What you just need to do now is follow along because we want to make our character move.
And if we want to make our character move, we will need a speed.
So let's go ahead and create a public float speed with a speed of five, for example.
So as I said, no worries.
And if you don't understand what's going on precisely, then we need another variable which is of type
rigid body and I'm going to call it my rigid body and within the start method, I'm going to initialize
it.
What that means is that I will set a value to my rigid body because otherwise it's empty and it doesn't
know what to do.
And within the start method is a perfect position to do that because that one is called right at the
beginning when our game object, which is our player, will be created within the game and here I choose
would get component rigid body.
All right.
So as I said, no worries.
If you don't understand what's going on, just follow along.
Then within the update method, I want to move my character vertically.
So I'm going to get the input of the user, get access, and I take the vertical axis and I do the same
thing for the horizontal axis.
So float move horizontal
is input dot get axis and this one is Hori zone tool.
And finally, I want to move my character vector three movement new vector three.
Move horizontal, then don't move it upwards and move vertical.
So what that means is in the update method, which is called once per frame, as it said here, my player
will check every time.
Is there a user input?
So does the player press.
For example, the vertical keys, which means does he press forward and backward or W and SW?
That will give us a vertical value.
So it will tell us, okay, he moves forward, that's one or he moves backward, that's minus one.
And the same thing is for the horizontal one.
So is he moving left or right that will be saved in here?
And now we create a 3D vector, which means it's a direction.
We say, okay, there is a direction which is a forward direction, which is a upward direction and
which is a sideward direction.
So this one is x move.
Horizontal is x, this one is our Y and this one is our Z.
And horizontal and vertical is from a specific perspective, we will see what that means.
And now finally, we can move our rigid body.
I will show you in a second what the rigid body is, and we will cover the rigid body in more detail
in one of the next videos.
So what we do now is we take the movement, which is the direction, and we multiply speed.
Whatever speed we have entered here, we will now move our player whenever we press buttons.
So let's just save that script and let's add that script to our player.
And there are multiple ways you can drag the script right from here, from your assets to the player
and the player movement script will be added in the inspector.
Otherwise you can go to add component and then search for scripts, and then you can find the player
movement.
You can press on it and you will add it.
If you accidentally have the same script twice, you can get rid of it by pressing on this top right
button and remove the component.
Now, before we run the game, let's position our player a little bit upwards and so that it's just
on top of the plane.
As you can see, it's it's just a well, we can even put it a bit higher.
All right.
And then next, we need to give our sphere a rigid body, and that one you can find under physics,
rigid body.
What that does is it gives a physical body to our object, which is in this case, our player.
And it has, for example, a mass.
It has a drag, an angular drag.
It can tell us whether it uses gravity or not.
And there are some further information we will look into Rigid Body in one of the next videos in detail.
But for now, let's just run the code.
And you can see if I move left, right and forward, I can move my ball.
Now, I don't like the angle of our camera, so we can easily change that.
Let's go to our main camera and now let's move it a little bit around.
As you can see here, we can move it towards this little this angle here, and we can change the rotation,
for example, so we could change the rotation.
And now we would have a different, very different look to our game from our.
Camera.
Now, that makes it a bit more difficult.
So let's set the rotation back to zero.
Let's move our camera a little bit to the side.
So here you have the camera preview.
Now let's rotate it over here.
And well, maybe a good position should be minus one five Z position can stay where it is and the rotation.
Now let's go with 45 degrees.
So now we have a different angle at our game.
And as you can see, there is my ball and whenever I'm off my playing field, it will fall off.
So as soon as it's off the underground, it will drop.
And that has to do with it having a gravity.
So our player has a gravity.
And that has to do with use gravity here.
So if I deactivate that, let's go ahead and look at it and let's see.
As you can see, our ball doesn't need the underground anymore.
And now it's somewhere we can't see it.
Actually, there it's back.
What you can see is if I push to the left and now I can't see it anymore.
There it is.
And I push back to the right.
It needs a little while until it goes back to the direction that I want to go.
And that has to do with it being a physical body or having a physical body, which makes it pretty darn
heavy.
So it's not that easy to change the direction with just a little force that we are applying to it.
All right.
So that's the basics of player movement.
We will have a look at more detailed pieces of the game later on.
Now, let's first have a look at the physics, because what we will do is we will have a look at physics
in the next videos.
We will have a look at scripting and the following chapter.
And afterwards you will have a much better feeling for what's going on and you will be able to create
your first little games.
So for now.
The player movement is a script that we have created.
We have written some lines of code.
You might not understand all of it.
No worries.
We'll get there.
The only thing that you need to understand is that you need to have a script in order to change the
behaviour of your game objects, which is our player, for example, or our underground and so forth.