Search Results
8/10/2025, 8:26:12 PM
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float sprintSpeed;
public float gravity = 9.9f;
public float jumpheight = 5.0f;
private PlayerControls controls;
private Vector2 moveInput;
private Vector3 jumpInput;
public Transform groundCheck;
public float groundDistance = 0.15f;
public LayerMask groundMask;
Vector3 velocity;
private Rigidbody rb;
private bool isGrounded;
private void Awake()
{
controls = new PlayerControls();
rb = GetComponent<Rigidbody>();
controls.Player.Move.performed += ctx => {
moveInput = ctx.ReadValue<Vector2>();
};
controls.Player.Move.canceled += ctx => {
moveInput = Vector2.zero;
};
controls.Player.Jump.performed += ctx => {
if (isGrounded)
{
velocity.y = Mathf.Sqrt(jumpheight * 2f * gravity); // <-- use this in FixedUpdate
}
};
}
private void OnEnable()
{
controls.Enable();
controls.Player.Enable(); // ensures the action map is active
}
private void OnDisable()
{
controls.Disable();
}
private void OnDrawGizmosSelected()
{
if (groundCheck == null) return;
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundDistance);
}
>1/2 of my movement code
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float sprintSpeed;
public float gravity = 9.9f;
public float jumpheight = 5.0f;
private PlayerControls controls;
private Vector2 moveInput;
private Vector3 jumpInput;
public Transform groundCheck;
public float groundDistance = 0.15f;
public LayerMask groundMask;
Vector3 velocity;
private Rigidbody rb;
private bool isGrounded;
private void Awake()
{
controls = new PlayerControls();
rb = GetComponent<Rigidbody>();
controls.Player.Move.performed += ctx => {
moveInput = ctx.ReadValue<Vector2>();
};
controls.Player.Move.canceled += ctx => {
moveInput = Vector2.zero;
};
controls.Player.Jump.performed += ctx => {
if (isGrounded)
{
velocity.y = Mathf.Sqrt(jumpheight * 2f * gravity); // <-- use this in FixedUpdate
}
};
}
private void OnEnable()
{
controls.Enable();
controls.Player.Enable(); // ensures the action map is active
}
private void OnDisable()
{
controls.Disable();
}
private void OnDrawGizmosSelected()
{
if (groundCheck == null) return;
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundDistance);
}
>1/2 of my movement code
8/9/2025, 10:50:35 PM
Page 1