59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class CameraFollower : MonoBehaviour
|
|
{
|
|
public bool autoOffset = true;
|
|
public Transform target;
|
|
public float minFOV = 11;
|
|
public float FOVmultiplier = 5;
|
|
public float smoothness = 0.1f;
|
|
public CameraUpdateMode updateMode;
|
|
public static CameraFollower instance{get; private set;}
|
|
public static void UpdateFrame()=> instance.HandleFrame();
|
|
void Awake(){
|
|
instance=this;
|
|
}
|
|
void Start()
|
|
{
|
|
if(target==null){return;}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
if(updateMode!= CameraUpdateMode.Fixed){return;}
|
|
HandleFrame();
|
|
}
|
|
|
|
void LateUpdate(){
|
|
if(updateMode!= CameraUpdateMode.Late){return;}
|
|
HandleFrame();
|
|
}
|
|
|
|
void Update(){
|
|
if(updateMode!= CameraUpdateMode.Normal){return;}
|
|
HandleFrame();
|
|
}
|
|
|
|
public void HandleFrame(){
|
|
if(target==null){return;}
|
|
if(Mathf.Abs(target.position.x) > 1000 || Mathf.Abs(target.position.y )> 1000){return;}
|
|
transform.position = Vector3.Lerp(transform.position, new Vector3(target.position.x, target.position.y,-10), smoothness * Time.deltaTime);
|
|
GetComponent<Camera>().orthographicSize = minFOV + ((target.localScale.x - 1) * FOVmultiplier);
|
|
}
|
|
|
|
public void SetTarget(Transform Target){
|
|
target = Target;
|
|
}
|
|
|
|
public enum CameraUpdateMode{
|
|
Fixed,
|
|
Late,
|
|
Normal,
|
|
Manual
|
|
}
|
|
|
|
}
|