Adjusting the Camera and Adding Obstacles

Tutorial

·

Beginner

·

+10XP

·

30 mins

·

(191)

Unity Technologies

Adjusting the Camera and Adding Obstacles

You will make the camera follow the player’s movements and add obstacles that the player can interact with in the game

Tutorial Outcome

By the end of this tutorial, you will be able to

  • Make camera follow the player
  • Add obstacles to a game
  • Make player interact with obstacles

Key concepts:

  • Camera Follow Script
  • Polygon Collider 2D

Challenge Ideas:

  • Add more than one obstacle in the game by adding more asteroids (remember you can find them in Project > Images > Asteroids)

Languages available:

1. Adjusting the Camera


While selecting Main Camera on the Hierarchy

While selecting Main Camera on the Hierarchy


You’ll tweak the view frame and make the camera follow an object by adding the Camera Follow script


  1. Select the Main Camera in the Hierarchy and in the Inspector window, change the Frame Size value until you’re happy with how it appears on the screen

  1. While still selecting the Main Camera on the Hierarchy, Go to Inspector and Click Add Camera Follow Script

  1. Drag and drop the SpaceshipRed from the Hierarchy into the Inspector > Camera Follow (Script) > Object to follow > Target > empty box

  1. Press play and notice how the camera now follows the red spaceship when you move it

drag and drop SpaceshipRed into the Target

drag and drop SpaceshipRed into the Target


Did you know?


  • When you use Playground, the Unity Interface is simplified and made easier to read for beginners. If you ever want to use the original Unity interface, you can go to the top left hand screen and click Playground > Turn Playground Off

2. Add Obstacles

When you go to Hierarchy and click on the asteroid and look in the Inspector window, the Polygon Collider 2D must be there.
Make sure you also check that when you click on SpaceshipRed and then go to Inspector, you also see the Polygon Collider 2D there

When you go to Hierarchy and click on the asteroid and look in the Inspector window, the Polygon Collider 2D must be there. Make sure you also check that when you click on SpaceshipRed and then go to Inspector, you also see the Polygon Collider 2D there


Have fun with physics and collisions and add obstacles into your game


  1. Go to Project > Images > Asteroids and select any asteroid and drag it onto Hierarchy

  1. In the Inspector, click on Add Component and add a Rigidbody 2D and set the Gravity to 0 and the Friction to 1 to ensure that the asteroid will stop moving after a short distance

  1. To allow the spaceship and the asteroid to be able to push each other, add the Polygon Collider 2D to both the asteroid and the spaceship

Complete this tutorial