Camera Moves With CharacterHow to get the camera to follow the character in Unity

Author Waldo
Published April 16, 2018

In this quick tutorial I explain how to get your Unity Camera to follow the character on the screen using a simple C# script. I also show you how to offset that camera so you can then position the character to a certain part of the screen.

Video Walkthrough

  • 1:00 - Create a C# script
  • 1:58 - Assign a follow object
  • 3:15 - Keep the Z coordinate the same
  • 5:40 - Setup camera offset variables

Complete Source Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraScript : MonoBehaviour {

    public Transform followObject;
    private Vector3 moveTemp;
    public float offsetY = 2;
    public float offsetX = 2;

    // Use this for initialization
    void Start () {
        moveTemp = followObject.transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        moveTemp = followObject.transform.position;
        moveTemp.y += offsetY;
        moveTemp.x += offsetX;
        moveTemp.z = transform.position.z;
        transform.position = moveTemp;
    }
}

This tutorial is sponsored by this community

In order to stick to our mission of keeping education free, our videos and the content of this website rely on the support of this community. If you have found value in anything we provide, and if you are able to, please consider contributing to our Patreon. If you can’t afford to financially support us, please be sure to like, comment and share our content — it is equally as important.

Join The Community

Discussion

Browse Tutorials About