Hello.
I am doing a game in style “Roll a ball”. In this script I want to make one jump of my ball when it is on ground. But if I press the space quickly, the ball jumps two or three times, bouncing not from the ground, but from the air. It’s physically impossible… Please help!
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class SphereController : MonoBehaviour
{
public Rigidbody rb;
public bool isGrounded;
public float jumpForce = 2.0f;
public Vector3 jump;
void Start ()
{
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
}