2024-04-20 01:11:54 +02:00
using UnityEngine ;
using Cinemachine ;
2024-04-20 20:55:22 +02:00
using System.Collections ;
2024-04-20 01:11:54 +02:00
public class CameraController : MonoBehaviour
{
2024-04-21 09:19:38 +02:00
public static CameraController instance ;
2024-04-20 16:52:39 +02:00
[SerializeField] private GameObject cam ;
2024-04-20 01:11:54 +02:00
2024-04-21 03:22:56 +02:00
public float scrollSpeed = 2.5f ;
2024-04-20 22:16:40 +02:00
[Range(5f, 30f)] public float stopAfterTime ;
2024-04-20 20:55:22 +02:00
public AnimationCurve curve = new AnimationCurve ( new Keyframe ( 0f , 0f , 1f , 1f ) , new Keyframe ( 1f , 1f , 1f , 1f ) ) ;
2024-04-20 01:11:54 +02:00
2024-04-21 03:22:56 +02:00
public Vector2 sens = new Vector2 ( 300f , 3f ) ;
2024-04-20 16:52:39 +02:00
private Vector2 defaultMaxSpeed ;
2024-04-20 01:11:54 +02:00
2024-04-20 20:55:22 +02:00
private float timer ;
2024-04-21 09:19:38 +02:00
private void Awake ( )
{
if ( instance = = null )
instance = this ;
}
2024-04-20 16:52:39 +02:00
private void Start ( )
{
2024-04-21 09:19:38 +02:00
if ( instance ! = this )
instance = this ;
2024-04-20 16:52:39 +02:00
defaultMaxSpeed * = 0 ;
}
2024-04-20 01:11:54 +02:00
2024-04-20 16:52:39 +02:00
private void Update ( )
{
2024-04-20 20:55:22 +02:00
//defaultMaxSpeed = new Vector2(cam.GetComponent<CinemachineFreeLook>().m_XAxis.m_MaxSpeed, cam.GetComponent<CinemachineFreeLook>().m_YAxis.m_MaxSpeed);
2024-04-20 01:11:54 +02:00
2024-04-20 16:52:39 +02:00
if ( Input . GetMouseButtonDown ( 1 ) )
{
defaultMaxSpeed = sens ;
2024-04-20 20:55:22 +02:00
// Enable mouse input
cam . GetComponent < CinemachineFreeLook > ( ) . m_XAxis . m_InputAxisName = "Mouse X" ;
cam . GetComponent < CinemachineFreeLook > ( ) . m_YAxis . m_InputAxisName = "Mouse Y" ;
2024-04-20 16:52:39 +02:00
}
if ( Input . GetMouseButtonUp ( 1 ) )
{
2024-04-20 20:55:22 +02:00
timer = 0 ;
// Disable mouse input
cam . GetComponent < CinemachineFreeLook > ( ) . m_XAxis . m_InputAxisName = "" ;
cam . GetComponent < CinemachineFreeLook > ( ) . m_YAxis . m_InputAxisName = "" ;
2024-04-20 16:52:39 +02:00
}
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 ;
2024-04-20 20:55:22 +02:00
2024-04-21 04:37:42 +02:00
cam . GetComponent < CinemachineCameraOffset > ( ) . m_Offset . z = Mathf . Clamp ( cam . GetComponent < CinemachineCameraOffset > ( ) . m_Offset . z , - 20 , 10 ) ;
2024-04-20 20:55:22 +02:00
timer + = Time . deltaTime ;
float evalTime = timer / stopAfterTime ;
if ( ! Input . GetMouseButton ( 1 ) )
defaultMaxSpeed = Vector2 . LerpUnclamped ( defaultMaxSpeed , Vector2 . zero , curve . Evaluate ( evalTime ) ) ;
2024-04-20 16:52:39 +02:00
}
2024-04-21 09:19:38 +02:00
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 ;
}
2024-04-20 01:11:54 +02:00
}