147 lines
4.1 KiB
C#
147 lines
4.1 KiB
C#
|
|
using NaughtyAttributes;
|
|||
|
|
using UnityEngine;
|
|||
|
|
public class CameraController : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
#region Variables
|
|||
|
|
#region Data
|
|||
|
|
[Space, Header("Data")]
|
|||
|
|
[SerializeField] private CameraInputData camInputData = null;
|
|||
|
|
|
|||
|
|
[Space, Header("Custom Classes")]
|
|||
|
|
[SerializeField] private CameraSwaying cameraSway = null;
|
|||
|
|
|
|||
|
|
[System.NonSerialized] public float x;
|
|||
|
|
[System.NonSerialized] public float y;
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
private float refX;
|
|||
|
|
private float refY;
|
|||
|
|
|
|||
|
|
#region Settings
|
|||
|
|
[Space, Header("Look Settings")]
|
|||
|
|
private const float controllerSensitivityMultiplier = 4000.0f;
|
|||
|
|
private const float aimingSensitivityMultiplier = 0.4f;
|
|||
|
|
[HideInInspector] public float sensitivity = 1.0f;
|
|||
|
|
[SerializeField] private Vector2 smoothAmount = Vector2.zero;
|
|||
|
|
[SerializeField][MinMaxSlider(-90f, 90f)] private Vector2 lookAngleMinMax = Vector2.zero;
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Private
|
|||
|
|
private float yawVelocity;
|
|||
|
|
private float pitchVelocity;
|
|||
|
|
private float m_yaw;
|
|||
|
|
private float m_pitch;
|
|||
|
|
|
|||
|
|
[HideInInspector] public float m_desiredYaw;
|
|||
|
|
[HideInInspector] public float m_desiredPitch;
|
|||
|
|
|
|||
|
|
#region Components
|
|||
|
|
private Transform m_pitchTranform;
|
|||
|
|
[HideInInspector] public Camera playerCam;
|
|||
|
|
#endregion
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region BuiltIn Methods
|
|||
|
|
void Awake()
|
|||
|
|
{
|
|||
|
|
GetComponents();
|
|||
|
|
InitValues();
|
|||
|
|
InitComponents();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Start()
|
|||
|
|
{
|
|||
|
|
Player.Instance.LockCursor(true);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void LateUpdate()
|
|||
|
|
{
|
|||
|
|
CalculateRotation();
|
|||
|
|
SmoothRotation();
|
|||
|
|
ApplyRotation();
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Custom Methods
|
|||
|
|
void GetComponents()
|
|||
|
|
{
|
|||
|
|
m_pitchTranform = transform.GetChild(0).transform;
|
|||
|
|
playerCam = GetComponentInChildren<Camera>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void InitValues()
|
|||
|
|
{
|
|||
|
|
m_yaw = transform.eulerAngles.y;
|
|||
|
|
m_desiredYaw = m_yaw;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public Ray CameraRay => new Ray(playerCam.transform.position, playerCam.transform.forward);
|
|||
|
|
|
|||
|
|
void InitComponents()
|
|||
|
|
{
|
|||
|
|
cameraSway.Init(playerCam.transform);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CalculateRotation()
|
|||
|
|
{
|
|||
|
|
if (Player.Instance.updatePlayer && Player.Instance.IsAlive())
|
|||
|
|
{
|
|||
|
|
bool usingGamePad = Player.Instance.GetInputHandler().UsingGamePad;
|
|||
|
|
float baseSensitivity = usingGamePad ? sensitivity * controllerSensitivityMultiplier * Time.fixedDeltaTime : sensitivity;
|
|||
|
|
float lookSpeed = Player.Instance.IsAiming ? baseSensitivity * aimingSensitivityMultiplier : baseSensitivity;
|
|||
|
|
|
|||
|
|
m_desiredYaw += camInputData.InputVectorX * lookSpeed;
|
|||
|
|
m_desiredPitch -= camInputData.InputVectorY * lookSpeed;
|
|||
|
|
|
|||
|
|
m_desiredPitch = Mathf.Clamp(m_desiredPitch, lookAngleMinMax.x, lookAngleMinMax.y);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SmoothRotation()
|
|||
|
|
{
|
|||
|
|
bool rawInput = SettingsMenu.Instance.ToggleRawInput;
|
|||
|
|
if (rawInput)
|
|||
|
|
{
|
|||
|
|
m_yaw = m_desiredYaw;
|
|||
|
|
m_pitch = m_desiredPitch;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
m_yaw = Mathf.SmoothDampAngle(m_yaw, m_desiredYaw, ref refX, smoothAmount.x);
|
|||
|
|
m_pitch = Mathf.SmoothDampAngle(m_pitch, m_desiredPitch, ref refY, smoothAmount.y);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ApplyRotation()
|
|||
|
|
{
|
|||
|
|
transform.root.eulerAngles = new Vector3(0f, m_yaw, 0f);
|
|||
|
|
m_pitchTranform.localEulerAngles = new Vector3(m_pitch, 0f, 0f);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void AddAimAssist(Vector3 direction)
|
|||
|
|
{
|
|||
|
|
//Transform cameraTransform = transform.root;
|
|||
|
|
|
|||
|
|
float yaw = Vector3.SignedAngle(Vector3.forward, direction, Vector3.up);
|
|||
|
|
//float pitch = Vector3.SignedAngle(cameraTransform.forward, direction, cameraTransform.right);
|
|||
|
|
|
|||
|
|
m_desiredYaw = Mathf.LerpAngle(m_desiredYaw, yaw, Time.fixedDeltaTime * 2);
|
|||
|
|
//m_desiredPitch = Mathf.LerpAngle(m_desiredPitch, pitch, Time.fixedDeltaTime * 2);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetRotation()
|
|||
|
|
{
|
|||
|
|
m_desiredYaw = m_yaw;
|
|||
|
|
m_desiredPitch = m_pitch;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void HandleSway(Vector3 _inputVector, float _rawXInput)
|
|||
|
|
{
|
|||
|
|
cameraSway.SwayPlayer(_inputVector, _rawXInput);
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|