510 lines
19 KiB
C#
510 lines
19 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Cinemachine;
|
|
using UnityStandardAssets.CrossPlatformInput;
|
|
using System.IO;
|
|
|
|
public class CameraMovement : MonoBehaviour
|
|
{
|
|
Animator cameraMovementAnimator;
|
|
public float _cameraSensitivity = 150f; //How quick is the sensitivity of the mouse .
|
|
public float _mouseSensitivity = 3.5f;
|
|
public float controllerSensitivity = 10f;
|
|
[HideInInspector] public float _mouseSensitivityDefault;
|
|
|
|
[SerializeField] private Transform _playerBody;
|
|
|
|
float xRotation = 0f;
|
|
|
|
private static CameraMovement _instance;
|
|
public static CameraMovement GetInstance() { return _instance; }
|
|
|
|
|
|
//Delete later: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
//TeamViewer:
|
|
public bool _teamViewerCameraInput;
|
|
public bool isMovingCameraX_Positive;
|
|
public bool isMovingCameraX_Negative;
|
|
|
|
float mouseX;
|
|
float mouseY;
|
|
|
|
private CinemachineVirtualCamera _CMVCamera;
|
|
private CinemachineBasicMultiChannelPerlin noise;
|
|
private float AmplitudeGainDefault;
|
|
private float FrequencyGainDefault;
|
|
public float cameraFieldOfViewDefault;
|
|
public float lerpFieldOfView;
|
|
public bool IsZooming;
|
|
public bool CanLerpValue = true;
|
|
public float lerpCameraRun;
|
|
private bool shouldStopCameraLerpWalk;
|
|
private bool shouldStopCameraLerpRun;
|
|
public bool cameraCanBeMovedByPlayer = true;
|
|
public float CameraRunElapsedTime;
|
|
public float CameraWalkElapsedTime;
|
|
public float cameraAngleMaxValueWalk;
|
|
public float cameraAngleMaxValueRunLastUpdate;
|
|
public float cameraAngleMaxValueRun = 60f;
|
|
public float cameraAngleMaxValueWalkForced = 75f;
|
|
public float cameraAngleMaxValueSitting = 65f;
|
|
|
|
[Header("If you want to lock the rotation of the camera:")]
|
|
public bool cameraCarMovement;
|
|
public bool cameraBedHideMovement;
|
|
public bool cameraSittingMovement;
|
|
public bool lockCameraRotation = false; // Add a bool to determine if camera rotation is locked
|
|
public float maxCameraYRotationAngle = -30f; // Set the maximum camera rotation angle
|
|
float yRotationForCar = 0f;
|
|
|
|
Coroutine lerpCameraWhenPlayerRunsCoroutine;
|
|
Coroutine lerpCameraWhenPlayerWalksCoroutine;
|
|
|
|
public SettingsData settingsData;
|
|
private string settingsPath;
|
|
|
|
void Awake()
|
|
{
|
|
if (!_instance)
|
|
{
|
|
_instance = this;
|
|
}
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
#region Try to load Settings for camera sensitivity
|
|
settingsPath = Application.persistentDataPath + "/settings.json";
|
|
if (File.Exists(settingsPath))
|
|
{
|
|
string json = File.ReadAllText(settingsPath);
|
|
settingsData = JsonUtility.FromJson<SettingsData>(json);
|
|
Debug.Log("Settings file exists");
|
|
_mouseSensitivity = settingsData.mouseCameraSensitivity;
|
|
controllerSensitivity = settingsData.controllerCameraSensitivity;
|
|
Debug.Log("Settings for camera sensitivity loaded");
|
|
}
|
|
#endregion
|
|
|
|
Cursor.lockState = CursorLockMode.Locked; //locks the cursors position to the center of the screen.
|
|
Cursor.visible = false;
|
|
|
|
_mouseSensitivityDefault = _cameraSensitivity;
|
|
|
|
_CMVCamera = GetComponent<CinemachineVirtualCamera>();
|
|
cameraFieldOfViewDefault = _CMVCamera.m_Lens.FieldOfView;
|
|
cameraMovementAnimator = GetComponent<Animator>();
|
|
|
|
if (_CMVCamera != null)
|
|
{
|
|
noise = _CMVCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
|
|
AmplitudeGainDefault = noise.m_AmplitudeGain;
|
|
FrequencyGainDefault = noise.m_FrequencyGain;
|
|
}
|
|
}
|
|
|
|
public void EnableCarCameraMovement()
|
|
{
|
|
cameraCarMovement = true;
|
|
lockCameraRotation = true;
|
|
}
|
|
|
|
public void DisableCarCameraMovement()
|
|
{
|
|
cameraCarMovement = false;
|
|
lockCameraRotation = false;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
#region Team Viewer
|
|
//if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
//{
|
|
// _teamViewerCameraInput = true;
|
|
//}
|
|
//if (Input.GetKeyDown(KeyCode.Alpha2))
|
|
//{
|
|
// _teamViewerCameraInput = false;
|
|
//}
|
|
#endregion
|
|
|
|
if (!InputControlManager.Getinstance().IsUsingJoystick)
|
|
{
|
|
_cameraSensitivity = _mouseSensitivity;
|
|
}
|
|
else
|
|
{
|
|
_cameraSensitivity = controllerSensitivity;
|
|
}
|
|
|
|
if (Time.timeScale > 0)
|
|
{
|
|
if (Input.GetKey(KeyCode.Mouse1) || Input.GetKey(KeyCode.JoystickButton4))
|
|
{
|
|
if (CanLerpValue)
|
|
{
|
|
lerpFieldOfView = 0f;
|
|
IsZooming = true;
|
|
StopAllCoroutines();
|
|
StartCoroutine(ZoomInFieldOfView());
|
|
CanLerpValue = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (IsZooming)
|
|
{
|
|
CanLerpValue = true;
|
|
lerpFieldOfView = 0f;
|
|
StopAllCoroutines();
|
|
StartCoroutine(ZoomOutFieldOfView());
|
|
IsZooming = false;
|
|
}
|
|
}
|
|
|
|
#region Origianl Input
|
|
if (!_teamViewerCameraInput)
|
|
{
|
|
if (!InputControlManager.Getinstance().IsUsingJoystick)
|
|
{
|
|
mouseX = Input.GetAxis("Mouse X") * _cameraSensitivity;
|
|
mouseY = Input.GetAxis("Mouse Y") * _cameraSensitivity;
|
|
}
|
|
else
|
|
{
|
|
mouseX = Input.GetAxis("Joystick X") * _cameraSensitivity;
|
|
mouseY = Input.GetAxis("Joystick Y") * _cameraSensitivity;
|
|
}
|
|
}
|
|
#endregion
|
|
#region Input RemoteControll Team Viewer
|
|
//if(_teamViewerCameraInput)
|
|
//{
|
|
//float mouseX = Input.mousePosition("Mouse X") * _mouseSensitivity;
|
|
//float mouseY = Input.mousePosition("Mouse Y") * _mouseSensitivity;
|
|
//}
|
|
if (_teamViewerCameraInput)
|
|
{
|
|
|
|
if (Input.GetKey(KeyCode.E))
|
|
{
|
|
mouseX += 35 * Time.deltaTime;
|
|
isMovingCameraX_Positive = true;
|
|
print("Is using MouseX Teamviewer input");
|
|
}
|
|
else
|
|
{
|
|
isMovingCameraX_Positive = false;
|
|
}
|
|
if (Input.GetKey(KeyCode.Q))
|
|
{
|
|
mouseX -= 35 * Time.deltaTime;
|
|
isMovingCameraX_Negative = true;
|
|
print("Is using -MouseX Teamviewer input");
|
|
}
|
|
else
|
|
{
|
|
isMovingCameraX_Negative = false;
|
|
}
|
|
if (!isMovingCameraX_Positive && !isMovingCameraX_Negative)
|
|
{
|
|
mouseX = Input.GetAxis("Mouse X") * _cameraSensitivity;
|
|
print("is not moving camera");
|
|
}
|
|
//Vector2 lastAxis = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
|
|
//Vector3 axis = new Vector3(-(lastAxis.x - Input.mousePosition.x) * 0.1f, -(lastAxis.y - Input.mousePosition.y) * 0.1f, Input.GetAxis("Mouse ScrollWheel"));
|
|
|
|
//mouseX = CrossPlatformInputManager.GetAxis("Mouse X") * _mouseSensitivity;
|
|
//mouseY = CrossPlatformInputManager.GetAxis("Mouse Y") * _mouseSensitivity;
|
|
|
|
//mouseX = axis.x * _mouseSensitivity;
|
|
//mouseY = axis.y * _mouseSensitivity;
|
|
}
|
|
#endregion
|
|
|
|
if (PlayerManager.GetInstance()._PlayerMovement._CharacterAnimator.GetFloat("speedPercent") > 0)
|
|
{
|
|
if (PlayerManager.GetInstance()._PlayerMovement.isWalking)
|
|
{
|
|
cameraMovementAnimator.SetBool("IsWalking", true);
|
|
cameraMovementAnimator.SetBool("IsRunning", false);
|
|
}
|
|
else if (!PlayerManager.GetInstance()._PlayerMovement.isWalking && !PlayerManager.GetInstance()._PlayerMovement._isRunning)
|
|
{
|
|
cameraMovementAnimator.SetBool("IsWalking", false);
|
|
cameraMovementAnimator.SetBool("IsRunning", false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cameraMovementAnimator.SetBool("IsWalking", false);
|
|
cameraMovementAnimator.SetBool("IsRunning", false);
|
|
}
|
|
|
|
if (cameraCanBeMovedByPlayer)
|
|
{
|
|
xRotation -= mouseY;
|
|
}
|
|
|
|
if (!PlayerManager.GetInstance()._PlayerMovement._isRunning && !PlayerManager.GetInstance()._PlayerMovement.isWalking)
|
|
{
|
|
if (!lockCameraRotation)
|
|
{
|
|
if (!cameraSittingMovement)
|
|
{
|
|
xRotation = Mathf.Clamp(xRotation, -90f, 80f); // How much the camera can turn in the y axis.
|
|
}
|
|
else
|
|
{
|
|
xRotation = Mathf.Clamp(xRotation, -90f, 65f); // How much the camera can turn in the y axis.
|
|
}
|
|
}
|
|
else
|
|
{
|
|
xRotation = Mathf.Clamp(xRotation, maxCameraYRotationAngle, 30f); // How much the camera can turn in the y axis.
|
|
}
|
|
|
|
lerpCameraRun = 0f;
|
|
//CameraRunElapsedTime = 0f;
|
|
//IEnumerator lerpCameraWhenPlayerRuns = LerpCameraWhenPlayerRuns();
|
|
//StopCoroutine(lerpCameraWhenPlayerRunsCoroutine);
|
|
|
|
if (CameraRunElapsedTime <= 0.1f && shouldStopCameraLerpRun)
|
|
{
|
|
StopCoroutine(lerpCameraWhenPlayerRunsCoroutine);
|
|
lerpCameraWhenPlayerRunsCoroutine = null;
|
|
CameraRunElapsedTime = 0f;
|
|
shouldStopCameraLerpRun = false;
|
|
cameraCanBeMovedByPlayer = true;
|
|
}
|
|
else
|
|
{
|
|
shouldStopCameraLerpRun = false;
|
|
}
|
|
|
|
if (CameraWalkElapsedTime <= 0.1f && shouldStopCameraLerpWalk)
|
|
{
|
|
StopCoroutine(lerpCameraWhenPlayerWalksCoroutine);
|
|
lerpCameraWhenPlayerWalksCoroutine = null;
|
|
CameraWalkElapsedTime = 0f;
|
|
shouldStopCameraLerpWalk = false;
|
|
cameraCanBeMovedByPlayer = true;
|
|
}
|
|
else
|
|
{
|
|
shouldStopCameraLerpWalk = false;
|
|
}
|
|
|
|
cameraAngleMaxValueWalk = xRotation;
|
|
cameraAngleMaxValueRunLastUpdate = xRotation;
|
|
}
|
|
else if (!PlayerManager.GetInstance()._PlayerMovement._isRunning && PlayerManager.GetInstance()._PlayerMovement.isWalking)
|
|
{
|
|
cameraAngleMaxValueRunLastUpdate = xRotation;
|
|
|
|
cameraMovementAnimator.SetBool("IsWalking", true);
|
|
cameraMovementAnimator.SetBool("IsRunning", false);
|
|
|
|
if (CameraRunElapsedTime <= 0.1f && shouldStopCameraLerpRun)
|
|
{
|
|
StopCoroutine(lerpCameraWhenPlayerRunsCoroutine);
|
|
lerpCameraWhenPlayerRunsCoroutine = null;
|
|
CameraRunElapsedTime = 0f;
|
|
shouldStopCameraLerpRun = false;
|
|
cameraCanBeMovedByPlayer = true;
|
|
}
|
|
else
|
|
{
|
|
shouldStopCameraLerpRun = false;
|
|
}
|
|
|
|
if (cameraAngleMaxValueWalk > 70f)
|
|
{
|
|
if (!shouldStopCameraLerpWalk)
|
|
{
|
|
lerpCameraWhenPlayerWalksCoroutine = StartCoroutine(LerpCameraWhenPlayerWalks());
|
|
}
|
|
//StartCoroutine(LerpCameraWhenPlayerRuns());
|
|
if (CameraWalkElapsedTime >= 0.1f)
|
|
{
|
|
xRotation = Mathf.Clamp(xRotation, -90f, cameraAngleMaxValueWalkForced); // How much the camera can turn in the y axis.
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!shouldStopCameraLerpWalk)
|
|
xRotation = Mathf.Clamp(xRotation, -90f, cameraAngleMaxValueWalkForced); // How much the camera can turn in the y axis.
|
|
}
|
|
}
|
|
else /*(PlayerManager.GetInstance()._PlayerMovement._isRunning && !PlayerManager.GetInstance()._PlayerMovement.isWalking)*/
|
|
{
|
|
print("Is running gurl");
|
|
|
|
cameraAngleMaxValueWalk = xRotation;
|
|
|
|
cameraMovementAnimator.SetBool("IsWalking", false);
|
|
cameraMovementAnimator.SetBool("IsRunning", true);
|
|
if (CameraWalkElapsedTime <= 0.1f && shouldStopCameraLerpWalk)
|
|
{
|
|
StopCoroutine(lerpCameraWhenPlayerWalksCoroutine);
|
|
lerpCameraWhenPlayerWalksCoroutine = null;
|
|
CameraWalkElapsedTime = 0f;
|
|
shouldStopCameraLerpWalk = false;
|
|
cameraCanBeMovedByPlayer = true;
|
|
}
|
|
else
|
|
{
|
|
shouldStopCameraLerpWalk = false;
|
|
}
|
|
|
|
if (cameraAngleMaxValueRunLastUpdate > 60f)
|
|
{
|
|
if (!shouldStopCameraLerpRun)
|
|
{
|
|
lerpCameraWhenPlayerRunsCoroutine = StartCoroutine(LerpCameraWhenPlayerRuns());
|
|
}
|
|
|
|
//StartCoroutine(LerpCameraWhenPlayerRuns());
|
|
if (CameraRunElapsedTime >= 0.1f)
|
|
{
|
|
xRotation = Mathf.Clamp(xRotation, -90f, cameraAngleMaxValueRun); // How much the camera can turn in the y axis.
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!shouldStopCameraLerpRun)
|
|
xRotation = Mathf.Clamp(xRotation, -90f, cameraAngleMaxValueRun); // How much the camera can turn in the y axis.
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if (!cameraCarMovement && !cameraBedHideMovement && !cameraSittingMovement)
|
|
{
|
|
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
|
_playerBody.Rotate(Vector3.up * mouseX); //This moves the players' body with the camera Input.
|
|
}
|
|
else if (cameraCarMovement && !cameraBedHideMovement && !cameraSittingMovement)//If car movement:
|
|
{
|
|
yRotationForCar += mouseX;
|
|
yRotationForCar = Mathf.Clamp(yRotationForCar, -125f, 125f);
|
|
transform.localRotation = Quaternion.Euler(xRotation, yRotationForCar, 0f);
|
|
}
|
|
else if (cameraBedHideMovement && !cameraCarMovement && !cameraSittingMovement)
|
|
{
|
|
yRotationForCar += mouseX;
|
|
yRotationForCar = Mathf.Clamp(yRotationForCar, -75f, 75f);
|
|
transform.localRotation = Quaternion.Euler(xRotation, yRotationForCar, 0f);
|
|
}
|
|
else if (cameraSittingMovement && !cameraCarMovement && !cameraBedHideMovement)
|
|
{
|
|
yRotationForCar += mouseX;
|
|
yRotationForCar = Mathf.Clamp(yRotationForCar, -50f, 50f);
|
|
transform.localRotation = Quaternion.Euler(xRotation, yRotationForCar, 0f);
|
|
print("Sitting camera");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UseCameraBedHideMovement()
|
|
{
|
|
cameraBedHideMovement = true;
|
|
}
|
|
|
|
public void UseCameraSittingMovement()
|
|
{
|
|
cameraSittingMovement = true;
|
|
}
|
|
|
|
public void UseRegularGameplayCamera()
|
|
{
|
|
cameraBedHideMovement = false;
|
|
cameraCarMovement = false;
|
|
cameraSittingMovement = false;
|
|
}
|
|
|
|
public IEnumerator LerpCameraWhenPlayerWalks()
|
|
{
|
|
cameraCanBeMovedByPlayer = false;
|
|
CameraWalkElapsedTime = 0f;
|
|
float lerpDuration = 0.1f; // You can adjust the duration as needed
|
|
shouldStopCameraLerpWalk = true;
|
|
while (CameraWalkElapsedTime < lerpDuration)
|
|
{
|
|
CameraWalkElapsedTime += Time.deltaTime;
|
|
float t = Mathf.Clamp01(CameraWalkElapsedTime / lerpDuration); // Ensure t is between 0 and 1
|
|
xRotation = Mathf.Lerp(70, cameraAngleMaxValueWalkForced, t);
|
|
print("Is lerping camera because player walks");
|
|
yield return null;
|
|
}
|
|
|
|
// Ensure that the final value is exactly the target value
|
|
xRotation = cameraAngleMaxValueWalkForced;
|
|
cameraCanBeMovedByPlayer = true;
|
|
}
|
|
|
|
public IEnumerator LerpCameraWhenPlayerRuns()
|
|
{
|
|
cameraCanBeMovedByPlayer = false;
|
|
CameraRunElapsedTime = 0f;
|
|
float lerpDuration = 0.1f; // You can adjust the duration as needed
|
|
shouldStopCameraLerpRun = true;
|
|
while (CameraRunElapsedTime < lerpDuration)
|
|
{
|
|
CameraRunElapsedTime += Time.deltaTime;
|
|
float t = Mathf.Clamp01(CameraRunElapsedTime / lerpDuration); // Ensure t is between 0 and 1
|
|
xRotation = Mathf.Lerp(60, cameraAngleMaxValueRun, t);
|
|
print("Is lerping camera because player runs");
|
|
yield return null;
|
|
}
|
|
|
|
// Ensure that the final value is exactly the target value
|
|
xRotation = cameraAngleMaxValueRun;
|
|
cameraCanBeMovedByPlayer = true;
|
|
}
|
|
|
|
public IEnumerator ZoomInFieldOfView()
|
|
{
|
|
while (lerpFieldOfView < 1f)
|
|
{
|
|
lerpFieldOfView += 0.5f * Time.deltaTime;
|
|
//_CMVCamera.fieldOfView = Mathf.Lerp(_CMVCamera.fieldOfView, 45f, lerpFieldOfView);
|
|
_CMVCamera.m_Lens.FieldOfView = Mathf.Lerp(_CMVCamera.m_Lens.FieldOfView, 45f, lerpFieldOfView);
|
|
print("Is zooming in");
|
|
yield return null;
|
|
}
|
|
}
|
|
public IEnumerator ZoomOutFieldOfView()
|
|
{
|
|
while (lerpFieldOfView < 1f)
|
|
{
|
|
lerpFieldOfView += 0.5f * Time.deltaTime;
|
|
//_CMVCamera.fieldOfView = Mathf.Lerp(_CMVCamera.fieldOfView, cameraFieldOfViewDefault, lerpFieldOfView);
|
|
_CMVCamera.m_Lens.FieldOfView = Mathf.Lerp(_CMVCamera.m_Lens.FieldOfView, cameraFieldOfViewDefault, lerpFieldOfView);
|
|
print("Is zooming out");
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
public void SetShake(float amplitude, float frequency)
|
|
{
|
|
if (noise != null)
|
|
{
|
|
noise.m_AmplitudeGain = amplitude;
|
|
noise.m_FrequencyGain = frequency;
|
|
}
|
|
}
|
|
|
|
public void SetShakeToDefault()
|
|
{
|
|
if (noise != null)
|
|
{
|
|
noise.m_AmplitudeGain = AmplitudeGainDefault;
|
|
noise.m_FrequencyGain = FrequencyGainDefault;
|
|
}
|
|
}
|
|
}
|