67 lines
2.7 KiB
C#
67 lines
2.7 KiB
C#
using UnityEngine;
|
|
using Fusion;
|
|
|
|
namespace OnlyScove.Scripts
|
|
{
|
|
public class PlayerMovement : NetworkBehaviour
|
|
{
|
|
[field: Header("Movement Settings")]
|
|
[field: SerializeField] public float WalkSpeed { get; private set; } = 3f;
|
|
[field: SerializeField] public float RunSpeed { get; private set; } = 6f;
|
|
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f;
|
|
[field: SerializeField] public float SneakSpeed { get; private set; } = 1.5f;
|
|
[field: SerializeField] public float DashForce { get; private set; } = 10f;
|
|
[field: SerializeField] public float RotationSpeed { get; private set; } = 500f;
|
|
|
|
[field: Header("Airborne Settings")]
|
|
[field: SerializeField] public float JumpHeight { get; private set; } = 2f;
|
|
[field: SerializeField] public float Gravity { get; private set; } = -15f;
|
|
[field: SerializeField] public float ThrustDownwardForce { get; private set; } = -20f;
|
|
|
|
[field: Header("Ground Check")]
|
|
[field: SerializeField] public float GroundCheckRadius { get; private set; } = 0.2f;
|
|
[field: SerializeField] public Vector3 GroundCheckOffset { get; private set; }
|
|
[field: SerializeField] public LayerMask GroundMask { get; private set; }
|
|
|
|
[Networked] public bool IsGrounded { get; set; }
|
|
[Networked] public bool WasGrounded { get; set; }
|
|
[Networked] public float VelocityY { get; set; }
|
|
|
|
private CharacterController controller;
|
|
|
|
public void Initialize(CharacterController controller)
|
|
{
|
|
this.controller = controller;
|
|
}
|
|
|
|
public void CheckGround(Transform playerTransform)
|
|
{
|
|
if (Object == null || (!Object.HasStateAuthority && !Object.HasInputAuthority)) return;
|
|
|
|
WasGrounded = IsGrounded;
|
|
IsGrounded = Physics.CheckSphere(playerTransform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
|
|
}
|
|
|
|
public void Move(CharacterController controller, Vector3 velocity, float deltaTime)
|
|
{
|
|
if (controller != null && controller.enabled)
|
|
{
|
|
controller.Move(velocity * deltaTime);
|
|
}
|
|
}
|
|
|
|
public void Rotate(Transform playerTransform, Vector3 moveDirection, float deltaTime)
|
|
{
|
|
if (moveDirection == Vector3.zero) return;
|
|
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
|
playerTransform.rotation = Quaternion.RotateTowards(playerTransform.rotation, targetRot, RotationSpeed * deltaTime);
|
|
}
|
|
|
|
public void SetGroundCheck(float radius, Vector3 offset)
|
|
{
|
|
GroundCheckRadius = radius;
|
|
GroundCheckOffset = offset;
|
|
}
|
|
}
|
|
}
|