This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Player/PlayerMovementController.cs

168 lines
4.8 KiB
C#
Raw Permalink Normal View History

2023-03-14 00:59:59 +01:00
using System.Collections;
using Cinemachine;
2023-06-01 17:03:48 +02:00
using UnityEngine;
2023-03-14 00:59:59 +01:00
public class PlayerMovementController : MonoBehaviour
{
2023-06-01 17:03:48 +02:00
[SerializeField] private PlayerAnimationController animcontroller;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[SerializeField] private CharacterController ccontroller;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[SerializeField] private CameraController cameraController;
2023-04-21 09:30:43 +02:00
2023-06-01 17:03:48 +02:00
[SerializeField] private float speed = 1f;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[SerializeField] private float sideSpeed = 0.5f;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[SerializeField] private float animatedRotationSpeed = 5f;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[SerializeField] public Camera cam;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[HideInInspector] public bool AllowRotation;
2023-04-21 09:30:43 +02:00
2023-06-01 17:03:48 +02:00
[HideInInspector] public bool IsAiming;
2023-04-21 09:30:43 +02:00
2023-06-01 17:03:48 +02:00
[SerializeField] private NoiseVisibilitySettingsManager noiseSettings;
2023-04-21 09:30:43 +02:00
2023-06-01 17:03:48 +02:00
[SerializeField] private CinemachineFreeLook freelook;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[SerializeField] private string mouseXAxis = "Mouse X";
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
[SerializeField] private string mouseYAxis = "Mouse Y";
2023-04-21 09:30:43 +02:00
2023-06-01 17:03:48 +02:00
public bool isDead;
private CharacterControllerForce ccForceAddon;
private float initXSensitivity = 250f;
2023-06-01 17:03:48 +02:00
private float initYSensitivity = 10f;
2023-06-01 17:03:48 +02:00
//private float mouseX = 0;
private bool isSneaking = false;
private Vector3 lookingDirectionVector;
private InGameManager manager;
private bool movementLocked;
private float x;
private float y;
public bool IsRunning { get; private set; }
public float NoiseDistance => noiseSettings.NoiseDistance;
// Start is called before the first frame update
private void Start()
{
ccForceAddon = ccontroller.gameObject.GetComponent<CharacterControllerForce>();
manager = FindObjectOfType<InGameManager>();
initXSensitivity = freelook.m_XAxis.m_MaxSpeed;
initYSensitivity = freelook.m_YAxis.m_MaxSpeed;
}
// Update is called once per frame
private void Update()
{
if (isDead) return;
AllowRotation = IsAiming || IsRunning;
IsAiming = Input.GetMouseButton(1) || Input.GetAxis("Aim") > 0.5f;
GetMovementOld();
MovePlayer();
animcontroller.Animate(new Vector2(x, y), false, IsRunning);
lookingDirectionVector = (cameraController.Forward * y + cameraController.Right * x).normalized;
if ((IsRunning && !Input.GetMouseButtonDown(1)) || AllowRotation)
{
if ((AllowRotation && x == 0 && y == 0) || (IsAiming && PlayerInteractionHandler.instance.GunEnabled))
lookingDirectionVector = cameraController.Forward.normalized;
SlowLookAt(lookingDirectionVector);
}
freelook.m_XAxis.m_InputAxisName = !manager.IsPaused ? mouseXAxis : "";
freelook.m_YAxis.m_InputAxisName = !manager.IsPaused ? mouseYAxis : "";
}
2023-03-14 00:59:59 +01:00
private void GetMovementOld()
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
2023-06-01 17:03:48 +02:00
if (movementLocked || manager.IsPaused)
2023-04-21 09:30:43 +02:00
{
x = 0;
y = 0;
}
2023-06-01 17:03:48 +02:00
IsRunning = Mathf.Abs(y) > 0.1f || Mathf.Abs(x) > 0.1f;
2023-03-14 00:59:59 +01:00
}
2023-06-01 17:03:48 +02:00
public void SetSensitivity(float sensitivity)
{
2023-06-01 17:03:48 +02:00
freelook.m_XAxis.m_MaxSpeed = initXSensitivity * sensitivity;
freelook.m_YAxis.m_MaxSpeed = initYSensitivity * sensitivity;
}
2023-06-01 17:03:48 +02:00
2023-03-14 00:59:59 +01:00
private void MovePlayer()
{
2023-06-01 17:03:48 +02:00
var movement = Vector3.zero;
2023-09-02 02:27:19 +02:00
//Modified to a normal 1 vector to ensure speed is not greater when moving diagonally.
Vector2 normalizedInput = new Vector2(x, y).normalized;
x = normalizedInput.x;
y = normalizedInput.y;
//movement += transform.forward * Mathf.Abs(y) * Time.deltaTime * speed;//+(transform.right*x*Time.deltaTime*speed);
2023-06-01 17:03:48 +02:00
movement += cam.transform.forward * y * Time.deltaTime * speed;
//movement += transform.forward * Mathf.Abs(x) * Time.deltaTime * speed;
if (!IsAiming)
movement += cam.transform.right * x * Time.deltaTime * speed;
else
movement += cam.transform.right * x * Time.deltaTime * sideSpeed;
2023-06-01 17:03:48 +02:00
movement += Vector3.down * Time.deltaTime * 9.8f;
2023-03-14 00:59:59 +01:00
ccontroller.Move(movement);
}
public void SetSpeed(float speed)
{
this.speed = speed;
}
private void GetJumpOld()
{
}
2023-06-01 17:03:48 +02:00
private void SlowLookAt(Vector3 targetVector)
2023-03-14 00:59:59 +01:00
{
2023-06-01 17:03:48 +02:00
var relativePos = targetVector;
var toRotation = Quaternion.LookRotation(relativePos);
2023-06-01 17:03:48 +02:00
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, animatedRotationSpeed * Time.deltaTime);
2023-03-14 00:59:59 +01:00
}
2023-04-21 09:30:43 +02:00
public void PhysicalForceMove(Vector3 vector)
{
2023-06-01 17:03:48 +02:00
ccontroller.Move(vector);
2023-04-21 09:30:43 +02:00
ccForceAddon.AddImpact(vector, vector.magnitude);
}
2023-06-01 17:03:48 +02:00
2023-04-21 09:30:43 +02:00
public void LockMovement(float duration)
{
StartCoroutine(lockMovement(duration));
}
2023-06-01 17:03:48 +02:00
2023-04-21 09:30:43 +02:00
private IEnumerator lockMovement(float duration)
{
movementLocked = true;
yield return new WaitForSeconds(duration);
movementLocked = false;
}
2023-06-01 17:03:48 +02:00
}