4 Game Animations
13K views
Oct 30, 2023
Unity3D Game Animations 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 fourth episode of this game development
0:04
series on c-sharp corner.com. So in the last tutorial, we made our dinosaur move in the X direction and jump in the Y direction
0:12
But we encountered an issue that our dinosaur was flying on continuously clicking the left mouse button
0:18
In this video we will try to fix that issue. So without wasting any further time, let's get started
0:28
So to fix this issue, we are going to make a check if our dinosaur is standing on the ground
0:33
only that it will be allowed to jump in the wide direction, otherwise if it is in the air
0:38
then the jumping is not going to be allowed. So to achieve that, we are going to create a new variable of Tybulion and we are going to
0:45
call it on ground. So whenever on ground equal to true, our dinosaur can make a jump and if on ground equal to
0:52
false, our dinosaur cannot make a jump. So now in order to achieve that, I am going to introduce you to end up
0:58
another Unity method called On Collision Enter 2D. This method is called whenever a game object gets collided with another game object
1:07
Also it takes the game object it has collided with as its parameter
1:11
So over here this variable collision is a reference to the game object our dinosaur has collided with So over here inside this method I going to make a check if our dinosaur has collided with ground or not If our dinosaur has collided with ground then we are going to mark on ground equal to true
1:31
To achieve that, we are going to write if collision. .collider. tag equal to ground
1:39
Now what are tags? Tag is a parameter which provides a set of classification to game objects
1:46
set that click on the ground go to the inspector panel click on the tag area over here
1:53
click on this click on add tag click on the plus sign to add a new custom tag I'm gonna add
2:00
ground and then again over here in this list you can see we have got the ground now I
2:05
will click on it and now our ground have got the ground tag and now I'm gonna set on
2:12
ground equal to true over here now there exists another function called on collision exit 2d which is called whenever the collision between the two
2:21
object ends so inside this i'm going to set on ground equal to false and now whenever we are
2:29
trying to make a jump over there we are going to add another condition if on ground equal to
2:34
equal to true and now if we hit play we are only able to make one jump great and now when we have achieved it let create some enemies for our dinosaur To do that let go to Sprite folder From here drag this cactus Sprite to the Hierarchy Panel change its position and size
2:54
let's rename this as Enemy, go to the Inspector Panel, click on Add Components, and add
2:59
the Box Collider and a Rigidbody 2D2. Now let's go to the Scripts folder and create a new script for our Enemy
3:07
Let's call it enemy script, double click on it to open it up
3:11
And this time we are completely going to remove start and update method from the script
3:16
So now let's talk what we want to achieve with this script
3:19
We want our enemy to kill the dinosaur whenever the dinosaur comes in contact with our enemy
3:25
So to do that, we can also use On Collision Enter 2D, but this time I want to introduce you
3:32
to another predefined unity method, which is called On, trigger, enter 2D
3:37
there is a big difference between collision and trigger. Collision means when two objects physically hits each other
3:44
and trigger means whenever they gets in contact but passes away from them
3:49
So now let's get back to our script. Over here I'm going to write on trigger enter 2D
3:55
Again this takes the game object it has triggered as a parameter
3:59
Over here we are going to check if the object it has triggered with have a tag of Dino So to do that I will make a check over here I will write if Collision equal to Dino then destroy collision
4:13
That means destroy the object it have triggered with. Now in the hierarchy panel, I will click on the Dino player
4:20
We'll go to tag, add a new tag, Dino, and we'll assign the Dino tag to our Dino player
4:26
And now we click on the enemy in the hierarchy panel. We go to the Inspector Panel and over here under the box Collider 2D we can see there is a check box called is trigger
4:37
We are going to check this so that it can encounter triggers. Inside the rigid body I'm going to change the body type from dynamic to kinematic, add the enemy script and we'll hit play to check what happens
4:49
And now as expected our dinosaur has got destroyed. In the hierarchy panel you can see there is no Dino Player game object left
4:57
Okay this is what we want to do. achieve. But this is not the coolest way of killing the dinosaur. So in the next video
5:05
we will learn the concept of sprite swapping through which we will be killing our dinosaur
5:10
So till then, good-bye, have fun and don't forget to follow and subscribe because the best
5:14
is yet to come
#Action & Platform Games
#Computer & Video Games
#Games
#Video Game Development