3DTD/Assets/Scripts/Camera/CameraController.cs

60 lines
2.0 KiB
C#

using UnityEngine;
using Cinemachine;
using System.Collections;
public class CameraController : MonoBehaviour
{
[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 Start()
{
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, -15f, 10f);
timer += Time.deltaTime;
float evalTime = timer / stopAfterTime;
if (!Input.GetMouseButton(1))
defaultMaxSpeed = Vector2.LerpUnclamped(defaultMaxSpeed, Vector2.zero, curve.Evaluate(evalTime));
}
}