mission-demolition/Assets/Scripts/FollowCam.cs

33 lines
1.3 KiB
C#
Raw Normal View History

2024-02-19 19:52:08 -06:00
using UnityEngine;
using System.Collections;
public class FollowCam : MonoBehaviour
{
static public GameObject POI; // The static point of interest
[Header("Set in Inspector")]
public float easing = 0.05f;
public Vector2 minXY = Vector2.zero;
[Header("Set Dynamically")]
public float camZ; // The desired Z pos of the camera
void Awake() {
camZ = this.transform.position.z;
}
void FixedUpdate() {
// if there's only one line following an if, it doesn't need braces
if (POI == null) return; // return if there is no poi
// Get the position of the poi
Vector3 destination = POI.transform.position;
// Limit the X & Y to minimum values
destination.x = Mathf.Max(minXY.x, destination.x);
destination.y = Mathf.Max(minXY.y, destination.y);
// Interpolate from the current Camera position toward destination
destination = Vector3.Lerp(transform.position, destination, easing);
// Force destination.z to be camZ to keep the camera far enough away
destination.z = camZ;
// Set the camera to the destination
transform.position = destination;
// Set the orthographicSize of the Camera to keep Ground in view
Camera.main.orthographicSize = destination.y + 10;
}
}