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.
With your circle selected in the Hierarchy, go over the right side of the screen to see the Inspector. Do the following:
- Rename your Circle to “Player”.
- Click “Add Component” and add a “Rigidbody 2D” (see below).
Rigidbody: Add Physics
A Rigidbody 2D enables your GameObject to have physics, such as gravity, drag, and mass.
- Check the “Use Auto Mass” flag because we will not need to worry about mass in our physics.
- Check the “Freeze Rotation Z” so the circle doesn’t rotate in strange ways when it moves.
- Click “Add Component” to add a “Circle Collider 2D”.
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.).
Falling Ball
Press “Play” to see the ball fall.
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.
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.