157 lines
4.2 KiB
C#
157 lines
4.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Cinemachine;
|
|
|
|
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;
|
|
|
|
private InGameManager manager;
|
|
|
|
|
|
public float NoiseDistance { get { return this.noiseSettings.NoiseDistance; } }
|
|
|
|
|
|
private Vector3 lookingDirectionVector;
|
|
|
|
private bool movementLocked = false;
|
|
[SerializeField]
|
|
private CinemachineFreeLook freelook;
|
|
[SerializeField]
|
|
private string mouseXAxis = "Mouse X";
|
|
[SerializeField]
|
|
private string mouseYAxis = "Mouse Y";
|
|
|
|
public bool isDead = false;
|
|
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);
|
|
}
|
|
private void MovePlayer()
|
|
{
|
|
Vector3 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;
|
|
|
|
//movement += transform.forward * Mathf.Abs(x) * Time.deltaTime * speed;
|
|
movement += cam.transform.right * 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<CharacterControllerForce>();
|
|
manager = GameObject.FindObjectOfType<InGameManager>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(isDead) return;
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
freelook.m_XAxis.m_InputAxisName = !manager.IsPaused ? this.mouseXAxis : "";
|
|
freelook.m_YAxis.m_InputAxisName = !manager.IsPaused ? this.mouseYAxis : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
}
|