54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using Mirror;
|
|
using Player.States;
|
|
using StateMachine;
|
|
using UnityEngine;
|
|
|
|
namespace Player
|
|
{
|
|
public class Movement : MonoBehaviour
|
|
{
|
|
public float walkSpeed = 400f;
|
|
public float runSpeed = 1200f;
|
|
public float jumpForce = 160f;
|
|
public float groundCheckRadius = 0.14f;
|
|
public Transform groundCheck;
|
|
public LayerMask groundLayer;
|
|
|
|
[HideInInspector] public Rigidbody playerRigidbody;
|
|
[HideInInspector] public bool isGrounded;
|
|
[HideInInspector] public Vector3 moveDirection;
|
|
|
|
private float _originalHeight;
|
|
|
|
void Start()
|
|
{
|
|
playerRigidbody = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
|
|
moveDirection = (transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical")).normalized;
|
|
if (isGrounded && Input.GetButtonDown("Jump"))
|
|
{
|
|
playerRigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
|
|
}
|
|
|
|
Vector3 velocity = moveDirection * (walkSpeed * Time.deltaTime);
|
|
|
|
if (Cursor.lockState == CursorLockMode.Locked)
|
|
{
|
|
playerRigidbody.velocity = new Vector3(velocity.x, playerRigidbody.velocity.y, velocity.z);
|
|
}
|
|
}
|
|
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
if (groundCheck != null)
|
|
{
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
|
|
}
|
|
}
|
|
}
|
|
} |