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

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.

In this video you will learn how to:

  • 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;
    }
}

LINK TO THIS POST

SHARE THIS POST


DISCUSSION