How to Make a Ball (Game Object) Jump on Mouse click in Unity
Notes:
How to Make a Ball (Game Object) Jump on Mouse click in Unity:
Example Code:
using UnityEngine;
public class BallController : MonoBehaviour {
Rigidbody rb;
public bool isGrounded;
public float forceY;
void Start () {
rb = this.GetComponent<Rigidbody> ();
isGrounded = false;
forceY = 0f;
}
void Update () {
if(isGrounded==true)
{
if(Input.GetMouseButton(0))
{
forceY = forceY + 100f * Time.deltaTime;
}
else if (Input.GetMouseButtonUp (0)) {
rb.AddForce (new Vector3 (0f, forceY, 300f));
forceY = 0f;
}
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Ground") {
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name == "Ground") {
isGrounded = false;
}
}
}