70 lines
2.0 KiB
C#
70 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;
|
||
|
[HideInInspector] 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");
|
||
|
|
||
|
animator.SetFloat("x", Input.GetAxis("Horizontal"));
|
||
|
animator.SetFloat("y", Input.GetAxis("Vertical"));
|
||
|
|
||
|
if (isGrounded && Input.GetButtonDown("Jump"))
|
||
|
{
|
||
|
StateMachine.SetCurrentState(PlayerState.Jump);
|
||
|
}
|
||
|
|
||
|
StateMachine.Update();
|
||
|
|
||
|
Debug.Log(StateMachine.GetCurrentState());
|
||
|
}
|
||
|
|
||
|
void FixedUpdate()
|
||
|
{
|
||
|
StateMachine.FixedUpdate();
|
||
|
}
|
||
|
|
||
|
void OnDrawGizmosSelected()
|
||
|
{
|
||
|
if (groundCheck != null)
|
||
|
{
|
||
|
Gizmos.color = Color.red;
|
||
|
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|