using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class Movement : MonoBehaviour { [Header("Movement Settings")] public float acceleration = 1f; // Amount to increase speed per W tap public float deceleration = 0.5f; // Amount to decrease speed per second public float maxSpeed = 10f; // Maximum speed public float turnSpeed = 100f; // Degrees per second for turning public float hopForce = 5f; // Upward force for the bunny hop public float groundCheckDistance = 0.1f; // Distance to check for ground private float currentSpeed = 0f; private Rigidbody rb; private Collider col; void Start() { rb = GetComponent(); col = GetComponent(); rb.freezeRotation = true; // Prevent physics from tipping it over } void Update() { // Increase speed while holding W if (Input.GetKey(KeyCode.W)) { currentSpeed += acceleration * Time.deltaTime; currentSpeed = Mathf.Min(currentSpeed, maxSpeed); } // Only decelerate when W is NOT pressed else { currentSpeed -= deceleration * Time.deltaTime; currentSpeed = Mathf.Max(currentSpeed, 0f); } // Turning is still rotation of transform if (Input.GetKey(KeyCode.A)) { transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime); } if (Input.GetKey(KeyCode.D)) { transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime); } // Bunny hop on space bar if grounded if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()) { rb.AddForce(Vector3.up * hopForce, ForceMode.Impulse); } } void FixedUpdate() { // Move forward in FixedUpdate for consistent physics Vector3 forwardMove = transform.forward * currentSpeed; Vector3 newVelocity = new Vector3(forwardMove.x, rb.velocity.y, forwardMove.z); rb.velocity = newVelocity; } private bool IsGrounded() { // Improved ground check using collider bounds return Physics.Raycast(col.bounds.center, Vector3.down, col.bounds.extents.y + groundCheckDistance); } }