95 lines
2.9 KiB
C#
95 lines
2.9 KiB
C#
using UnityEngine;
|
|
using Cinemachine;
|
|
using System.Collections;
|
|
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
public static CameraController instance;
|
|
|
|
[SerializeField] private GameObject cam;
|
|
|
|
public float scrollSpeed = 2.5f;
|
|
[Range(5f, 30f)] public float stopAfterTime;
|
|
public AnimationCurve curve = new AnimationCurve(new Keyframe(0f, 0f, 1f, 1f), new Keyframe(1f, 1f, 1f, 1f));
|
|
|
|
public Vector2 sens = new Vector2(300f, 3f);
|
|
private Vector2 defaultMaxSpeed;
|
|
|
|
private float timer;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance == null)
|
|
instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (instance != this)
|
|
instance = this;
|
|
|
|
defaultMaxSpeed *= 0;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//defaultMaxSpeed = new Vector2(cam.GetComponent<CinemachineFreeLook>().m_XAxis.m_MaxSpeed, cam.GetComponent<CinemachineFreeLook>().m_YAxis.m_MaxSpeed);
|
|
|
|
if (Input.GetMouseButtonDown(1))
|
|
{
|
|
defaultMaxSpeed = sens;
|
|
|
|
// Enable mouse input
|
|
cam.GetComponent<CinemachineFreeLook>().m_XAxis.m_InputAxisName = "Mouse X";
|
|
cam.GetComponent<CinemachineFreeLook>().m_YAxis.m_InputAxisName = "Mouse Y";
|
|
}
|
|
|
|
if (Input.GetMouseButtonUp(1))
|
|
{
|
|
timer = 0;
|
|
|
|
// Disable mouse input
|
|
cam.GetComponent<CinemachineFreeLook>().m_XAxis.m_InputAxisName = "";
|
|
cam.GetComponent<CinemachineFreeLook>().m_YAxis.m_InputAxisName = "";
|
|
}
|
|
|
|
cam.GetComponent<CinemachineFreeLook>().m_XAxis.m_MaxSpeed = defaultMaxSpeed.x;
|
|
cam.GetComponent<CinemachineFreeLook>().m_YAxis.m_MaxSpeed = defaultMaxSpeed.y;
|
|
|
|
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
|
|
|
cam.GetComponent<CinemachineCameraOffset>().m_Offset.z += scroll * scrollSpeed;
|
|
|
|
cam.GetComponent<CinemachineCameraOffset>().m_Offset.z = Mathf.Clamp(cam.GetComponent<CinemachineCameraOffset>().m_Offset.z, -20, 10);
|
|
|
|
timer += Time.deltaTime;
|
|
float evalTime = timer / stopAfterTime;
|
|
if (!Input.GetMouseButton(1))
|
|
defaultMaxSpeed = Vector2.LerpUnclamped(defaultMaxSpeed, Vector2.zero, curve.Evaluate(evalTime));
|
|
}
|
|
|
|
public void ShakeCamera(float intensity, float duration)
|
|
{
|
|
StartCoroutine(DoShake(intensity, duration));
|
|
}
|
|
|
|
private IEnumerator DoShake(float intensity, float duration)
|
|
{
|
|
Vector3 originalOffset = cam.GetComponent<CinemachineCameraOffset>().m_Offset;
|
|
float elapsed = 0.0f;
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
float x = Random.Range(-1f, 1f) * intensity;
|
|
float y = Random.Range(-1f, 1f) * intensity;
|
|
|
|
cam.GetComponent<CinemachineCameraOffset>().m_Offset = new Vector3(x, y, originalOffset.z);
|
|
|
|
elapsed += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
cam.GetComponent<CinemachineCameraOffset>().m_Offset = originalOffset;
|
|
}
|
|
}
|