Below is my script. I want to make and Object jump with Physics, but only when it’s grounded. It works well except when 2 forces combine - the bounce of and the impulse I apply. Then the ball goes higher then it should. I wonder if there some smart trick to prevent that?
public class Movement : MonoBehaviour
{
public float jumpForce;
Rigidbody m_Rigidbody;
Vector3 m_NewForce;
bool isGrounded;
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
m_NewForce = new Vector3(0.0f, 3.0f, 0.0f);
isGrounded = false;
}
// Update is called once per frame
void FixedUpdate()
{
var v = Input.GetAxis("Vertical");
if(v > 0f && isGrounded)
{
m_Rigidbody.AddForce(m_NewForce, ForceMode.Impulse);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}