2024-10-17 23:09:51 +03:00

67 lines
2.0 KiB
C#

using Mirror;
using Player.States;
using StateMachine;
using UnityEngine;
namespace Player
{
public class Player : MonoBehaviour
{
public float walkSpeed = 400f;
public float runSpeed = 1200f;
public float jumpForce = 160f;
public float groundCheckRadius = 0.14f;
public Transform groundCheck;
public LayerMask groundLayer;
public Animator animator;
public readonly StateMachine<PlayerState> StateMachine = new StateMachine<PlayerState>();
[HideInInspector] public Rigidbody playerRigidbody;
public bool isGrounded;
[HideInInspector] public Vector3 moveDirection;
private float _originalHeight;
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
StateMachine.Add(new Walk(this));
StateMachine.Add(new Run(this));
StateMachine.Add(new Jump(this));
StateMachine.SetCurrentState(PlayerState.Walk);
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
moveDirection = (transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical")).normalized;
animator.SetFloat("x", Input.GetAxis("Horizontal"));
animator.SetFloat("y", Input.GetAxis("Vertical"));
if (isGrounded && Input.GetButtonDown("Jump"))
{
Debug.Log("JUMP");
StateMachine.SetCurrentState(PlayerState.Jump);
}
StateMachine.Update();
}
void FixedUpdate()
{
StateMachine.FixedUpdate();
}
void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
}
}