55 lines
1.7 KiB
C#
Raw Normal View History

2024-10-17 23:09:51 +03:00
using UnityEngine;
namespace Player
{
public class Movement : MonoBehaviour
{
2024-10-19 15:47:54 +03:00
public float walkSpeed;
public float runSpeed;
2024-10-17 23:09:51 +03:00
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>();
}
2024-10-19 15:47:54 +03:00
private Vector3 _velocity;
2024-10-17 23:09:51 +03:00
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);
}
2024-10-19 15:47:54 +03:00
_velocity = moveDirection * (walkSpeed * Time.fixedDeltaTime);
}
2024-10-17 23:09:51 +03:00
2024-10-19 15:47:54 +03:00
private void FixedUpdate()
{
2024-10-17 23:09:51 +03:00
if (Cursor.lockState == CursorLockMode.Locked)
{
2024-10-19 15:47:54 +03:00
playerRigidbody.velocity = new Vector3(_velocity.x, playerRigidbody.velocity.y, _velocity.z);
2024-10-17 23:09:51 +03:00
}
}
2024-10-19 15:47:54 +03:00
2024-10-17 23:09:51 +03:00
void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
}
}