67 lines
2.0 KiB
C#
Raw Normal View History

2024-10-17 17:23:05 +03:00
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;
2024-10-17 23:09:51 +03:00
public bool isGrounded;
2024-10-17 17:23:05 +03:00
[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);
2024-10-17 23:09:51 +03:00
moveDirection = (transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical")).normalized;
2024-10-17 17:23:05 +03:00
animator.SetFloat("x", Input.GetAxis("Horizontal"));
animator.SetFloat("y", Input.GetAxis("Vertical"));
if (isGrounded && Input.GetButtonDown("Jump"))
{
2024-10-17 23:09:51 +03:00
Debug.Log("JUMP");
2024-10-17 17:23:05 +03:00
StateMachine.SetCurrentState(PlayerState.Jump);
}
2024-10-17 23:09:51 +03:00
StateMachine.Update();
2024-10-17 17:23:05 +03:00
}
void FixedUpdate()
{
StateMachine.FixedUpdate();
}
void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
}
}