Select your ball in the Hierarchy.
In the Inspector:
Add Rigidbody (if it doesn't have one).
Make sure Use Gravity is checked.
Add a Collider (Sphere Collider is fine).
Right-click in the Project window → Create → C# Script
Name it: BallJump
Attach the script to your ball.
using UnityEngine;
3. Use this script
public class BallJump : MonoBehaviour
{
public float jumpForce = 5f;
private Rigidbody rb;
private bool isGrounded = false;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
private void OnCollisionEnter(Collision collision)
{
// Check if we hit the ground
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
}
Click the ground object
Inspector → Tag → Add Tag… → create Ground
Set its tag to Ground