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

167 lines
4.6 KiB
C#
Raw Normal View History

2023-03-14 00:59:59 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
2023-03-14 00:59:59 +01:00
public class PlayerMovementController : MonoBehaviour
{
[SerializeField]
private PlayerAnimationController animcontroller;
private float x = 0;
private float y = 0;
//private float mouseX = 0;
2023-03-14 00:59:59 +01:00
private bool isRunning = false;
2023-04-21 09:30:43 +02:00
private bool isSneaking = false;
public bool IsRunning { get { return isRunning; } }
2023-03-14 00:59:59 +01:00
[SerializeField]
private CharacterController ccontroller;
2023-04-21 09:30:43 +02:00
private CharacterControllerForce ccForceAddon;
2023-03-14 00:59:59 +01:00
[SerializeField]
private CameraController cameraController;
[SerializeField]
private float speed = 1f;
[SerializeField]
private float sideSpeed = 0.5f;
[SerializeField]
private float animatedRotationSpeed = 5f;
2023-04-08 07:23:11 +02:00
[SerializeField]
public Camera cam;
2023-03-14 00:59:59 +01:00
2023-04-10 04:30:54 +02:00
[HideInInspector]
public bool AllowRotation = false;
2023-03-14 00:59:59 +01:00
2023-04-21 09:30:43 +02:00
[SerializeField]
private NoiseVisibilitySettingsManager noiseSettings;
private InGameManager manager;
2023-04-21 09:30:43 +02:00
public float NoiseDistance { get { return this.noiseSettings.NoiseDistance; } }
2023-03-14 00:59:59 +01:00
private Vector3 lookingDirectionVector;
2023-04-21 09:30:43 +02:00
private bool movementLocked = false;
[SerializeField]
private CinemachineFreeLook freelook;
private float initXSensitivity = 250f;
private float initYSensitivity = 10f;
[SerializeField]
private string mouseXAxis = "Mouse X";
[SerializeField]
private string mouseYAxis = "Mouse Y";
public bool isDead = false;
2023-03-14 00:59:59 +01:00
private void GetMovementOld()
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
if (movementLocked||manager.IsPaused)
2023-04-21 09:30:43 +02:00
{
x = 0;
y = 0;
}
2023-03-14 00:59:59 +01:00
isRunning = (Mathf.Abs(y) > 0.1f || Mathf.Abs(x) > 0.1f);
}
public void SetSensitivity(float sensitivity)
{
this.freelook.m_XAxis.m_MaxSpeed = initXSensitivity * sensitivity;
this.freelook.m_YAxis.m_MaxSpeed= initYSensitivity * sensitivity;
}
2023-03-14 00:59:59 +01:00
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;
2023-03-21 19:49:47 +01:00
movement += Vector3.down * 9.8f;
2023-03-14 00:59:59 +01:00
ccontroller.Move(movement);
}
public void SetSpeed(float speed)
{
this.speed = speed;
}
private void GetJumpOld()
{
}
// Start is called before the first frame update
void Start()
{
2023-04-21 09:30:43 +02:00
ccForceAddon = ccontroller.gameObject.GetComponent<CharacterControllerForce>();
manager = GameObject.FindObjectOfType<InGameManager>();
initXSensitivity =freelook.m_XAxis.m_MaxSpeed;
initYSensitivity= freelook.m_YAxis.m_MaxSpeed;
2023-03-14 00:59:59 +01:00
}
// Update is called once per frame
void Update()
{
if(isDead) return;
AllowRotation = Input.GetMouseButton(1) || Input.GetAxis("Aim")>0.5f||IsRunning;
2023-03-14 00:59:59 +01:00
GetMovementOld();
MovePlayer();
animcontroller.Animate(new Vector2(x, y), false,isRunning);
this.lookingDirectionVector = ((cameraController.Forward * y) + cameraController.Right * x).normalized;
2023-04-10 04:30:54 +02:00
if (isRunning && !Input.GetMouseButtonDown(1)||AllowRotation)
2023-03-14 00:59:59 +01:00
{
2023-04-10 04:30:54 +02:00
if (AllowRotation && x == 0 && y == 0)
{
this.lookingDirectionVector = cameraController.Forward.normalized;
}
2023-03-14 00:59:59 +01:00
SlowLookAt(this.lookingDirectionVector);
}
2023-04-21 09:30:43 +02:00
2023-04-21 09:30:43 +02:00
freelook.m_XAxis.m_InputAxisName = !manager.IsPaused ? this.mouseXAxis : "";
freelook.m_YAxis.m_InputAxisName = !manager.IsPaused ? this.mouseYAxis : "";
2023-03-14 00:59:59 +01:00
}
2023-04-21 09:30:43 +02:00
2023-03-14 00:59:59 +01:00
void SlowLookAt(Vector3 targetVector)
{
Vector3 relativePos = targetVector;
Quaternion toRotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, animatedRotationSpeed * Time.deltaTime);
}
2023-04-21 09:30:43 +02:00
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;
}
2023-03-14 00:59:59 +01:00
}