In this part we’ll create the main game mechanics. As I mentioned in the introduction, we need to save the pandas by eliminating the snakes in each level which is done by sending them to the screen limit that we’ve created in the first part. At the same time we can click on penguins and make them disappear (destroy the penguin objects).
Let’s start by adding the Panda object:
1) Search for “panda” in the project tab then drag one of the sprites and drop it into the scene view. Call the newly created object: “Panda”.
2) Re-size the “Panda” object to (X = 0.5, Y = 0.5, Z = 0.5) so that it fits our demo level and place it on top of the platforms.
3) In the inspector, Add Component -> “2D Box Collider” to add a collider for the “Panda” object. Make sure that “Is Trigger” is un-checked.
So how are we going to make the objects fall? The answer is the “Rigidbody 2D” component. And how does it work exactly?
4) In the inspector, Add Component -> “Rigidbody 2D”. This rigidbody component makes the object that it’s attached to affected by gravity.
So when you play the game at this stage, you will notice that the “Panda” object starts falling down till it reaches the platforms and it stops. The rigidbody puts the object under the control of the physics engine so the gravity pulls the “Panda” object down till it stops when the “Panda” object collider collides with the platform and since both of the colliders have “Is Trigger” un-checked, then both of them won’t ignore this collision and that’s how the “Panda” object stopped falling. We don’t want the platforms that we’ve created in the first part to be affected by gravity so we didn’t add a rigidbody to them. Adding a rigidbody to the object also means that we can control the object by applying forces from the scripts (which we will probably cover in future tutorials). However, if “Is Kinematic” is checked then the rigidbody will stop reacting to collisions and applied forces.
The rigidbody component has a few more settings (gravity scale, mass, etc…) that you can see in details here.
Now let’s move the “Panda” object so that it’s not on top of the platforms anymore and hit the play button. You will notice that the object falls till it gets destroyed. But how? It’s because the object reached the screen limit which destroys the object that enters in its collider.
Enough playing with the Panda, let’s add the rest of the objects. Simply repeat the first steps above to create both the Snake and the Penguin objects.
Now duplicate the Penguin object twice and put all the objects just like in the screenshot below:
Next, we’ll create the script that will handle all these different objects and which will allow us to destroy the “Penguin” objects by clicking on them.
1) Right-click in the project tab, create a new C# script and call it “ObjectManager”.
2) Open the script and let’s add our first variables:
public bool ClickToDestroy = false; //If true, then the object that includes this script is marked as the penguin (gets destroyed when clicked).
public bool ObjToKeep = false; //If true, then the object that includes this script is marked as the panda (the object that we must protect: that mustn't fall in the screen limit).
public bool ObjToEliminate = false; //If true, then the object that includes this script is marked as the snake (the object that we must eliminate: that must fall in the screen limit to win).
3) Add the “OnMouseDown()” message below to the script. This message is only called when the player has pressed the object that includes this script. We first check if it’s the penguin object (that can be destroyed when clicked), if yes, then we destroy it. If not, nothing happens.
void OnMouseDown() //Player has pressed the object.
if(ClickToDestroy == true) //If we can destroy this object by clicking on it (penguin object).
Destroy(gameObject); //Destroy the penguin object.
4) Add the “ObjectManager” script to each one of the animal objects in the scene: Set “Click To Destroy” to true for the penguins, “ObjToKeep” for the panda, “ObjToEliminate” for the snake.
You can also add more and more objects of these 3 types to make different levels, just make sure you add the “ObjectManager” script to each one of them and set the object type from the inspector.
Now if you play the game, you can click on the penguins to destroy them. If you destroy the penguin in the right, the panda object falls down to the screen limit and if you click on the penguin in the left, the snake object falls down to the screen limit and gets destroyed.But there’s no indication that we’ve won or lost the game by protecting the panda object or failing to protect it. And that’s what we’re going to do in the next part!
Please don’t hesitate to ask any question and share your suggestions in the comments below.