using System.Collections; using Cinemachine; using UnityEngine; public class PlayerMovementController : MonoBehaviour { [SerializeField] private PlayerAnimationController animcontroller; [SerializeField] private CharacterController ccontroller; [SerializeField] private CameraController cameraController; [SerializeField] private float speed = 1f; [SerializeField] private float sideSpeed = 0.5f; [SerializeField] private float animatedRotationSpeed = 5f; [SerializeField] public Camera cam; [HideInInspector] public bool AllowRotation; [HideInInspector] public bool IsAiming; [SerializeField] private NoiseVisibilitySettingsManager noiseSettings; [SerializeField] private CinemachineFreeLook freelook; [SerializeField] private string mouseXAxis = "Mouse X"; [SerializeField] private string mouseYAxis = "Mouse Y"; public bool isDead; private CharacterControllerForce ccForceAddon; private float initXSensitivity = 250f; private float initYSensitivity = 10f; //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(); manager = FindObjectOfType(); 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 : ""; } 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; } public void SetSensitivity(float sensitivity) { freelook.m_XAxis.m_MaxSpeed = initXSensitivity * sensitivity; freelook.m_YAxis.m_MaxSpeed = initYSensitivity * sensitivity; } private void MovePlayer() { var movement = Vector3.zero; //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); 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; movement += Vector3.down * Time.deltaTime * 9.8f; ccontroller.Move(movement); } public void SetSpeed(float speed) { this.speed = speed; } private void GetJumpOld() { } private void SlowLookAt(Vector3 targetVector) { var relativePos = targetVector; var toRotation = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, animatedRotationSpeed * Time.deltaTime); } public void PhysicalForceMove(Vector3 vector) { ccontroller.Move(vector); ccForceAddon.AddImpact(vector, vector.magnitude); } public void LockMovement(float duration) { StartCoroutine(lockMovement(duration)); } private IEnumerator lockMovement(float duration) { movementLocked = true; yield return new WaitForSeconds(duration); movementLocked = false; } }