Link Search Menu Expand Document

Creating a Player

What is a GameObject?

Once your 2D Unity Project is open, you will be taken to the Scene view. On the left is the Hierarchy where all of your GameObjects will be. GameObjects are items in your scene, and everything is a GameObject. Right now, all that’s there is the Main Camera.

Create Player GameObject

Right-click the “SampleScene” GameObject and select GameObject > 2D Object > Sprites > Circle. Yes, our player will be a circle. We’ll add graphics later in another project.

Create Circle

With your circle selected in the Hierarchy, go over the right side of the screen to see the Inspector. Do the following:

  1. Rename your Circle to “Player”.
  2. Click “Add Component” and add a “Rigidbody 2D” (see below).

Circle Rename

Rigidbody: Add Physics

A Rigidbody 2D enables your GameObject to have physics, such as gravity, drag, and mass.

  1. Check the “Use Auto Mass” flag because we will not need to worry about mass in our physics.
  2. Check the “Freeze Rotation Z” so the circle doesn’t rotate in strange ways when it moves.
  3. Click “Add Component” to add a “Circle Collider 2D”.

Circle Rigidbody2d

Add Collider

A Collider enables your GameObject to collide with other GameObjects. We’ll keep all the default settings for our Circle Collider 2D. Note that there are different shaped colliders for different jobs (Capsule Colliders are great for characters with textures, etc.).

Circle Collider

Falling Ball

Press “Play” to see the ball fall.

Circle Rigidbody Play

Camera Track the Ball

Let’s make it so the camera tracks the player wherever they go. In the Hierarchy, simply drag the Main Camera to the Player GameObject. That will make the camera a “child” of the player, meaning that it will move along with it.

Camera as Child

It looks like the ball is not falling, but that’s because the camera is falling with it. Notice how the “Y” position (yellow box) is decreasing as the ball and camera fall together.

Camera Play