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

public class CameraFollow : MonoBehaviour
{
    [SerializeField]
    private Transform target;

    private float distanceToFollow;

    // Start is called before the first frame update
    void Start()
    {
        // Calculate follow distance offset on x-axis
        // Camera's x position - target's (Player) x position
        distanceToFollow = transform.position.x - target.position.x;
    }

    private void LateUpdate()
    {
        // Get the current position of the target
        float targetX = target.position.x;

        // Get the current position of the camera
        Vector3 cameraPosition = transform.position;

        // Update the position on x
        cameraPosition.x = targetX + distanceToFollow;

        // Update the camera's new position
        transform.position = cameraPosition;
    }
}
