2023-03-14 00:59:59 +01:00
|
|
|
using System.Collections;
|
2023-04-22 09:18:21 +02:00
|
|
|
using Cinemachine;
|
2023-06-01 17:03:48 +02:00
|
|
|
using UnityEngine;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
namespace Player
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
public class PlayerMovementController : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField] private PlayerAnimationController animcontroller;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private CharacterController ccontroller;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private CameraController cameraController;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private float speed = 1f;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private float sideSpeed = 0.5f;
|
2023-04-21 09:30:43 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private float animatedRotationSpeed = 5f;
|
2023-04-21 09:30:43 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] public Camera cam;
|
2023-04-21 09:30:43 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[HideInInspector] public bool AllowRotation;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[HideInInspector] public bool IsAiming;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private NoiseVisibilitySettingsManager noiseSettings;
|
2023-04-21 09:30:43 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private CinemachineFreeLook freelook;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private string mouseXAxis = "Mouse X";
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
[SerializeField] private string mouseYAxis = "Mouse Y";
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
public bool isDead;
|
|
|
|
private CharacterControllerForce ccForceAddon;
|
|
|
|
private float initXSensitivity = 250f;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private float initYSensitivity = 10f;
|
|
|
|
//private float mouseX = 0;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private bool isSneaking = false;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private Vector3 lookingDirectionVector;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private Game.InGameManager manager;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private bool movementLocked;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private float x;
|
|
|
|
private float y;
|
|
|
|
public bool IsRunning { get; private set; }
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
public float NoiseDistance => noiseSettings.NoiseDistance;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
// Start is called before the first frame update
|
|
|
|
private void Start()
|
2023-06-01 17:03:48 +02:00
|
|
|
{
|
2023-06-01 20:25:46 +02:00
|
|
|
ccForceAddon = ccontroller.gameObject.GetComponent<CharacterControllerForce>();
|
|
|
|
manager = FindObjectOfType<Game.InGameManager>();
|
|
|
|
initXSensitivity = freelook.m_XAxis.m_MaxSpeed;
|
|
|
|
initYSensitivity = freelook.m_YAxis.m_MaxSpeed;
|
2023-06-01 17:03:48 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
// 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();
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
animcontroller.Animate(new Vector2(x, y), false, IsRunning);
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
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-04-21 09:30:43 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private void GetMovementOld()
|
|
|
|
{
|
|
|
|
x = Input.GetAxis("Horizontal");
|
|
|
|
y = Input.GetAxis("Vertical");
|
|
|
|
if (movementLocked || manager.IsPaused)
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
IsRunning = Mathf.Abs(y) > 0.1f || Mathf.Abs(x) > 0.1f;
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
public void SetSensitivity(float sensitivity)
|
|
|
|
{
|
|
|
|
freelook.m_XAxis.m_MaxSpeed = initXSensitivity * sensitivity;
|
|
|
|
freelook.m_YAxis.m_MaxSpeed = initYSensitivity * sensitivity;
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private void MovePlayer()
|
|
|
|
{
|
|
|
|
var movement = Vector3.zero;
|
|
|
|
//movement += transform.forward * Mathf.Abs(y) * Time.deltaTime * speed;//+(transform.right*x*Time.deltaTime*speed);
|
|
|
|
movement += cam.transform.forward * y * Time.deltaTime * speed;
|
2023-05-11 05:59:58 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
//movement += transform.forward * Mathf.Abs(x) * Time.deltaTime * speed;
|
2023-05-11 05:59:58 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
if (!IsAiming)
|
|
|
|
movement += cam.transform.right * x * Time.deltaTime * speed;
|
|
|
|
else
|
|
|
|
movement += cam.transform.right * x * Time.deltaTime * sideSpeed;
|
2023-05-29 06:16:05 +02:00
|
|
|
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
movement += Vector3.down * Time.deltaTime * 9.8f;
|
|
|
|
ccontroller.Move(movement);
|
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
public void SetSpeed(float speed)
|
|
|
|
{
|
|
|
|
this.speed = speed;
|
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private void GetJumpOld()
|
|
|
|
{
|
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private void SlowLookAt(Vector3 targetVector)
|
|
|
|
{
|
|
|
|
var relativePos = targetVector;
|
|
|
|
var toRotation = Quaternion.LookRotation(relativePos);
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 20:25:46 +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
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
public void PhysicalForceMove(Vector3 vector)
|
|
|
|
{
|
|
|
|
ccontroller.Move(vector);
|
|
|
|
ccForceAddon.AddImpact(vector, vector.magnitude);
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
public void LockMovement(float duration)
|
|
|
|
{
|
|
|
|
StartCoroutine(lockMovement(duration));
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private IEnumerator lockMovement(float duration)
|
|
|
|
{
|
|
|
|
movementLocked = true;
|
|
|
|
yield return new WaitForSeconds(duration);
|
|
|
|
movementLocked = false;
|
|
|
|
}
|
2023-04-21 09:30:43 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
}
|