How to Make a Ball (Game Object) Jump on Key press in Unity
Notes:
How to Make a Ball (Game Object) Jump on Key press in Unity:
Example Code:
using UnityEngine;
public class BallController : MonoBehaviour {
Rigidbody rb;
public bool isGrounded;
void Start () {
rb = this.GetComponent<Rigidbody>();
isGrounded = false;
}
void Update () {
if ( isGrounded==true && Input.GetKeyUp (KeyCode.Space)) {
rb.AddForce (new Vector3 (0f, 500f, 300f));
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Ground") {
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name == "Ground") {
isGrounded = false;
}
}
}
Note:
Ball Physic Material: 1, 1, 0.8, minimum, maximum