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

195 lines
6.0 KiB
C#
Raw Normal View History

2023-03-14 00:59:59 +01:00
using System.Collections;
using Cinemachine;
2023-06-01 17:03:48 +02:00
using UnityEngine;
using FishNet.Object;
using FishNet.Connection;
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
{
public class PlayerMovementController : NetworkBehaviour
2023-06-01 20:25:46 +02:00
{
[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
private GameObject camObject;
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-02 06:30:58 +02:00
[SerializeField]
private Scriptable.GameState 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
public override void OnStartClient()
{
base.OnStartClient();
if (base.IsOwner)
{
cam = GameObject.Find("CameraHidden").GetComponent<Camera>();
camObject = GameObject.Find("CM vcam1");
Debug.Log("Setting camera for " + transform.GetChild(0).GetChild(2).name + "with camera " + camObject.name);
camObject.GetComponent<CinemachineFreeLook>().Follow = transform;
camObject.GetComponent<CinemachineFreeLook>().LookAt = transform.GetChild(0).GetChild(2);
transform.GetChild(0).GetComponent<PlayerAnimationController>().hiddenCam = cam;
}
else
{
this.enabled = false;
}
}
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>();
2023-06-01 20:25:46 +02:00
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-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
/// <summary>
/// Gets called by PlayerComponent to change sensitivity.
/// </summary>
/// <param name="sensitivity"></param>
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-06-01 20:25:46 +02:00
//movement += transform.forward * Mathf.Abs(x) * Time.deltaTime * speed;
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-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-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
}