Preparing the player prefab

Download the Player.unitypackage file. Make sure your Unity Project is open first and then double-click on the Player.unitypackage file:

Preparing the player prefab

Unity displays a window with the assets we are importing to the project. You might ask yourself a question, "Why do I need all this? This is confusing." Don't worry about it too much now. I have prepared Player.prefab with some assets for our Jake. In this book, we will learn about programming the game and not about preparing game art. This is the main reason we will work with prepared assets. We will go through every prefab we are importing to understand how things work. However, you will write the code to control this prefab and create the game!

After pressing Import, you will notice a bunch of folders being created in Unity. We should have:

  • Animations: This folder contains all Unity animation files
  • Materials: This is for storing all materials and physics materials
  • Sprites: This is for storing all art sprite assets courtesy of http://www.kenney.nl/

Unity also imported the Player.prefab file we will be using in our scene. Have you noticed something wrong with the project structure? We have promised ourselves to never keep any files in the root of the Assets folder. Let's fix it by creating a Prefabs folder and then dragging Player.prefab inside the folder:

Preparing the player prefab

Now, we can add Player.prefab into our scene. It is best if you drag the prefab file and drop it on the Hierarchy view.

"Hey there, I'm Jake!" Just appeared in the Scene view. He is a cool-looking happy alien. However, we don't have any functionality programmed. This is your job. You will make him run, jump, collect stuff, and sadly also die. Excited now? I hope so.

Preparing the player prefab

Before we start writing the code, we will go though the components attached to the Player game object. Select the Player game object and look at the Inspector tab.

Let's talk about the components we have attached to our Player.prefab. I am deliberately skipping the Transform component, as you are probably aware of its functionality already.

Rigidbody2D

Please have a look at the Unity manual. According to the documentation, a Rigidbody2D component places an object under the control of the physics engine. So simple. We add the Rigidbody component to every object we want to enable physics behavior on.

Once you do that, your GameObject will obtain mass and will be affected by gravity. Press Play in Unity to see what will happen.

Yes, the poor alien is falling down endlessly. This is correct as we don't have any other GameObjects (such as ground) that Jake can stand on. We will add it soon.

CircleCollider2D

Colliders are necessary for physics objects to affect each other through collisions and triggers. Select the Player game object and zoom in on Jake in the Scene view. You will see a green circle. This is our 2D collider. The physics engine won't allow any other colliders to overlap with that circle. This means that, if we have a floor, for example, Jake will stand on it!

Let's test this.

Download and import FloorShort.unitypackage, drag the newly imported prefab to the scene, and place it underneath our character. Press Play and you will notice that Jake will drop and stay on the piece of floor we just imported.

CircleCollider2D

Notice when the colliders meet right under his left leg. This is the collision point. Jake seems to be a little bit sad at the moment. We will fix it by adding some functionality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset