Episode 3 Scripts Predefined and Input Methods
8K views
Oct 30, 2023
Unity 3D Scripts Predefined and Input Methods Background music - Song: Ehrling - You And Me (Vlog No Copyright Music) Music provided by Vlog No Copyright Music. Video Link: https://youtu.be/8HJSl7AiJNg
View Video Transcript
0:00
Hello guys, this is Vivek Sharma and welcome to the third episode of this game development
0:04
series on C-sharp-corner.com. In this video we will learn how we can add scripts to our project, we will learn about
0:10
basic predefined methods in Unity, vectors and input methods in Unity. So without wasting any further time, let's get started
0:23
So to begin with, first of all we will create a new folder inside our assets panel
0:27
We are going to rename it as scripts. And then we are going to create a new script inside the scripts folder
0:34
And let's rename it as dino script. Double click on it to open it up inside our text editor
0:39
I am using Visual Studio over here. Over here as you can see we have two pre-given methods
0:45
First is start. Another one is update. Start method is called only once in the beginning of the game
0:51
And update method is called on each and every frame continuously. So now let me give a brief of what we want to achieve over here
0:58
We want our dinosaur to keep moving in the x direction continuously and our dinosaur
1:04
to jump in the y direction whenever somebody hits the left mouse button or touches the screen
1:09
And we are going to achieve it by adding velocity in the x direction continuously and y direction occasionally
1:16
Velocity is a physics component and to assess that we will be needing a rigid body or a
1:21
a rigidbody2d in our case as we are creating a 2d game
1:25
So first of all, we will be declaring a private variable of type rigidbody2d and let's call it dino body
1:32
So now what we wanna do is get reference to the original rigidbody2d attached to our dinosaur and use it here So to do that I gonna write dino body equal to get component Rigidbody2d
1:47
This will get the component Rigidbody2d attached to our dinosaur gameobject. Remember that I'm writing this inside the start method because I want this to be called
1:57
only once whenever the game starts. Now we wanna move our dinosaur and for that let's learn a new concept called vector
2:05
So, generally, vectors are of two types, vector 2 and vector 3
2:10
Vector 2 contains two parameters, one is x value, another one is y value
2:15
And vector 3 contains three values, value in x, value in y and value in z
2:21
You can use vector 2 whenever you have two inputs x and y and can use vector 3 whenever
2:25
you have three inputs x, y and z. But also, in vector 3, if you leave the third value empty or assigns it equal to 0, it starts
2:34
to work as vector 2. So over here in our script what I'm gonna do is create a new variable of type
2:41
float and call it x velocity. This is going to be the velocity of our dinosaur in the x direction
2:46
and to move our dinosaur I will be adding velocity to our dino body. To do that I will write dino
2:52
body dot velocity equal to vector 3 or vector 2 if you want velocity in x direction and velocity
2:59
in y direction and of course this is a 2d game that's why I'm not gonna use the z value
3:05
so the velocity in x is going to be the variable we just declared which is x velocity but we
3:11
don't want to make any changes to the velocity in the y direction when the dinosaur is moving
3:15
constantly so over here for velocity in y direction I will use dino body We will get back to unity We click on dino player
3:26
Click on add component to add a new component. We'll search for dino script
3:31
We'll add that. And over here we can see an empty field for x velocity
3:37
I will give it a value of 5. And then I will hit play
3:42
And now as we can see our dinosaur is not just falling. it's also moving in the x direction. Now I will go back to my sprites folder and
3:50
will add two more sprites, one for the enemy and another one to create a ground
3:55
I will drag the ground sprite to the hierarchy panel and will change its size
4:01
to make it look like a ground. By the way you can also achieve it by going to the
4:04
inspector panel and changing the values inside the transform section. Now I will
4:09
quickly rename this to ground and now I will go back to the inspector panel. We
4:14
click on add component and we'll add a box collider and a rigid body to this as well but now if i hit
4:20
play what we see is our ground is also falling down and this is definitely something we don't
4:25
want to achieve so to fix this inside the rigid body 2d we will change the body type from dynamic
4:31
to kinematic and now if we hit play our ground is not falling and our dinosaur is moving in the
4:37
x direction perfect now comes the second phase of our dinosaurs movement so now
4:43
what we want to do is move our dinosaur in the y direction whenever somebody
4:47
clicks the left mouse button or taps on the screen so to do that first of all we
4:52
will create a new variable of type float and call it y velocity this is going to be the velocity of our dinosaur in the y direction and now inside the update method I gonna put a condition that if there is a left mouse button click or a tap then do this task
5:08
So over here I'm gonna write if input dot get mouse button down zero
5:13
Then add velocity to our dinosaur in the y direction over here
5:17
I have used zero as the parameter for get mouse button down because zero represents the left mouse button
5:24
if we write 1 it represents the right mouse button and if we write 2 it represents the
5:29
middle mouse button but we are going to use 0 for now and inside this if condition I'm going to write
5:35
dynobody.velocity equal to new vector 3 velocity in x and velocity in y this time velocity in y
5:44
is going to be y velocity and the velocity in x is going to be an unordered velocity
5:50
So that's why I'm gonna use dino body.velocity.x over here I will go back to unity click on dino player
5:58
And then over here we have another open field for y velocity
6:02
I'm gonna change its value to 5 And now as we can see our dinosaur is moving in the x direction
6:08
And whenever we hit the left mouse button it goes into the y direction
6:11
But here is a catch If it keeps on clicking the left mouse button our dinosaur starts to fly
6:17
And this is definitely not what we want to achieve. So in the next tutorial, we will try to fix this and we will be adding some enemies for
6:25
our dinosaur. So till then, goodbye, have fun and don't forget to follow and subscribe because the
6:30
best is to come
#Action & Platform Games
#Computer Education
#Other
#Video Game Development