55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using UnityEngine;
|
|
|
|
namespace Player
|
|
{
|
|
public class Movement : MonoBehaviour
|
|
{
|
|
public float walkSpeed;
|
|
public float runSpeed;
|
|
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>();
|
|
}
|
|
|
|
private Vector3 _velocity;
|
|
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);
|
|
}
|
|
|
|
_velocity = moveDirection * (walkSpeed * Time.fixedDeltaTime);
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |