using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovementController : MonoBehaviour { [SerializeField] private PlayerAnimationController animcontroller; private float x = 0; private float y = 0; private float mouseX = 0; private bool isRunning = false; private bool isSneaking = false; public bool IsRunning { get { return isRunning; } } [SerializeField] private CharacterController ccontroller; private CharacterControllerForce ccForceAddon; [SerializeField] private CameraController cameraController; [SerializeField] private float speed = 1f; [SerializeField] private float sideSpeed = 0.5f; [SerializeField] private float animatedRotationSpeed = 5f; [SerializeField] private Camera cam; [HideInInspector] public bool AllowRotation = false; [SerializeField] private NoiseVisibilitySettingsManager noiseSettings; public float NoiseDistance { get { return this.noiseSettings.NoiseDistance; } } private Vector3 lookingDirectionVector; private bool movementLocked = false; private void GetMovementOld() { x = Input.GetAxis("Horizontal"); y = Input.GetAxis("Vertical"); if (movementLocked) { x = 0; y = 0; } isRunning = (Mathf.Abs(y) > 0.1f || Mathf.Abs(x) > 0.1f); } private void MovePlayer() { Vector3 movement = Vector3.zero; movement += transform.forward * Mathf.Abs(y) * Time.deltaTime * speed;//+(transform.right*x*Time.deltaTime*speed); //movement += transform.right * Mathf.Abs(x) * Time.deltaTime * sideSpeed; movement += transform.forward * Mathf.Abs(x) * Time.deltaTime * speed; movement += Vector3.down * 9.8f; ccontroller.Move(movement); } public void SetSpeed(float speed) { this.speed = speed; } private void GetJumpOld() { } // Start is called before the first frame update void Start() { ccForceAddon = ccontroller.gameObject.GetComponent(); } // Update is called once per frame void Update() { AllowRotation = Input.GetMouseButton(1) || Input.GetAxis("Aim")>0.5f; GetMovementOld(); MovePlayer(); animcontroller.Animate(new Vector2(x, y), false,isRunning); this.lookingDirectionVector = ((cameraController.Forward * y) + cameraController.Right * x).normalized; if (isRunning && !Input.GetMouseButtonDown(1)||AllowRotation) { if (AllowRotation && x == 0 && y == 0) { this.lookingDirectionVector = cameraController.Forward.normalized; } SlowLookAt(this.lookingDirectionVector); } } void SlowLookAt(Vector3 targetVector) { Vector3 relativePos = targetVector; Quaternion toRotation = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, animatedRotationSpeed * Time.deltaTime); } public void PhysicalForceMove(Vector3 vector) { this.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; } }