Created
November 14, 2021 09:30
-
-
Save didacus/d66838fa248731b51994f5ac0162adac to your computer and use it in GitHub Desktop.
Unity - Camera controller to follow player
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class CameraController : MonoBehaviour { | |
public GameObject player; //Public variable to store a reference to the player game object | |
private Vector3 offset; //Private variable to store the offset distance between the player and camera | |
// Use this for initialization | |
void Start () | |
{ | |
//Calculate and store the offset value by getting the distance between the player's position and camera's position. | |
offset = transform.position - player.transform.position; | |
} | |
// LateUpdate is called after Update each frame | |
void LateUpdate () | |
{ | |
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance. | |
transform.position = player.transform.position + offset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment