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
2023-06-15 15:26:12 +05:30

196 lines
6.1 KiB
C#

using System.Collections;
using Cinemachine;
using UnityEngine;
using FishNet.Object;
using FishNet.Connection;
namespace Player
{
public class PlayerMovementController : NetworkBehaviour
{
[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;
private GameObject camObject;
[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;
[SerializeField]
private Scriptable.GameState manager;
private bool movementLocked;
private float x;
private float y;
public bool IsRunning { get; private set; }
public float NoiseDistance => noiseSettings.NoiseDistance;
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;
transform.GetChild(0).GetChild(2).GetComponent<CameraController>().cam = cam;
}
else
{
this.enabled = false;
}
}
// Start is called before the first frame update
private void Start()
{
ccForceAddon = ccontroller.gameObject.GetComponent<CharacterControllerForce>();
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;
}
/// <summary>
/// Gets called by PlayerComponent to change sensitivity.
/// </summary>
/// <param name="sensitivity"></param>
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;
//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;
}
}
}