using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float moveSpeed = 6f;
[SerializeField] private float jumpForce = 6f;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float groundCheckDistance = 0.15f;
private Rigidbody rb;
void Awake() => rb = GetComponent<Rigidbody>();
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 input = new Vector3(h, 0f, v);
if (input.sqrMagnitude > 1f) input.Normalize();
Vector3 velocity = input * moveSpeed;
#if UNITY_6000_0_OR_NEWER
rb.linearVelocity = new Vector3(velocity.x, rb.linearVelocity.y, velocity.z);
#else
rb.velocity = new Vector3(velocity.x, rb.velocity.y, velocity.z);
#endif
}
bool IsGrounded() =>
Physics.Raycast(transform.position, Vector3.down, groundCheckDistance + 0.05f, groundMask);
}
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float moveSpeed = 6f;
[SerializeField] private float jumpForce = 6f;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float groundCheckDistance = 0.15f;
private Rigidbody rb;
void Awake() => rb = GetComponent<Rigidbody>();
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 input = new Vector3(h, 0f, v);
if (input.sqrMagnitude > 1f) input.Normalize();
Vector3 velocity = input * moveSpeed;
#if UNITY_6000_0_OR_NEWER
rb.linearVelocity = new Vector3(velocity.x, rb.linearVelocity.y, velocity.z);
#else
rb.velocity = new Vector3(velocity.x, rb.velocity.y, velocity.z);
#endif
}
bool IsGrounded() =>
Physics.Raycast(transform.position, Vector3.down, groundCheckDistance + 0.05f, groundMask);
}