Tag Archives: camera

Making the camera follow the player in a 2D Unity game with code

This tutorial will show you how to write a script in C# code that will make the camera follow the player in a 2D Unity game. Watch the video below and then scroll down for the sample code.

Sample code

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

  public GameObject player;
  public float offset;
  private Vector3 playerPosition;
  public float offsetSmoothing;

  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
    playerPosition = new Vector3 (player.transform.position.x, transform.position.y, transform.position.z);
    if (player.transform.localScale.x > 0f) {
      playerPosition = new Vector3 (playerPosition.x + offset, playerPosition.y, playerPosition.z);
    }
    else {
      playerPosition = new Vector3(playerPosition.x - offset, playerPosition.y, playerPosition.z);
    }

    transform.position = Vector3.Lerp (transform.position, playerPosition, offsetSmoothing * Time.deltaTime);
  }
}

Next tutorial: Stopping the player from sticking to the edge of platforms and walls

PIP Map Effect in Unity

In this tutorial we will look at how to use a picture-in-picture (PIP) effect to display a map in the top corner of the screen. The map allows a player to see where they are located in the game world and where nearby landmarks, objects or other players are.

The image below shows a game with a first person view as the main camera, and a PIP effect showing the player’s location on the game map in the top right corner of the screen.

A picture-in-picture (PIP) map in a first-person view game.
A picture-in-picture (PIP) map in a first-person view game.

Watch the video below to see how you can add a PIP map effect to your game in Unity.

In this scene you can’t actually see the red sphere in the first person view because the character is inside the sphere. However, if you were using a third person view, you would probably see the sphere. In this case, you would want to hide the sphere on the first person camera and only show it on the map camera. This is possible to achieve with the use of layers which is what we will look at in the next tutorial.

Tip: You might want to have the object that represents a blip on the map to be higher so that it is still visible if a character goes inside a building or cave or under another object.

Go further

See if you can work out how to have the map camera follow the player around so that their blip doesn’t disappear off the map. This is something you will need to know if you have large scenes where the camera does not cover the entire scene.

Advanced Camera Techniques in Unity

In this tutorial we will look at how to add cameras to objects, how to set up a split screen camera system, and how to add a picture-in-picture (PIP) effect to your game. Scroll down to watch the video.

Adding cameras to objects are particularly useful for when you want to add a camera view to a character or game object eg. first person view inside a car.

Split screen camera systems are great for multiplayer games because you can have one player using the top half of the screen and another player using the bottom half of the screen. You could set up two cameras or you could have four cameras taking up the screen if you have four players in the game. Take a look at the example below.

Two players using split screen camera system.
Two players using split screen camera system.

Picture-in-picture (PIP) effects are great for maps and rear-camera views. For example, you could use a camera to display a map in the top corner of the screen so the player knows where they are located in the game’s world. You could also use a PIP effect to display a rear-view mirror so that a player can see behind their car in a racing game. Take a look at the example below.

A picture-in-picture (PIP) effect with a second camera view.
A picture-in-picture (PIP) effect with a second camera view.

We will also look at how to handle errors that you might see related to multiple audio listeners. To find out how to add cameras to objects, add split screen camera systems and PIP effects, watch the video below.

 

Cameras in Unity

This tutorial explains how to add cameras to your 3D game project in Unity. Cameras allow you to see scenes from different views and you can also attach cameras to the players in your game so that they see different views eg. first person view. Scroll down to watch the video.

A camera setup with the Camera Preview in Unity.
A camera view in the game preview mode
A camera view in the game preview mode

 

Watch the video below to see how you can add cameras to your own game project.