How to make ball jump when screen is tapped

Hi guys!

I just wanted to ask how to make a ball jump and fall back to the ground after jumping. I’ve seen some solutions already but I don’t know how to implement it on my codes. I’m making something like a roll-a-ball game on android. Here’s my codes:

void Update (){
	Vector3 tilt = new Vector3(Input.acceleration.x, 0, -Input.acceleration.z);
	
	if (Input.GetMouseButtonDown (0)) {
			//jump here
	}

	tilt = Quaternion.Euler (90,0,0) * tilt;
	rb.AddForce (tilt * 4000);
}

I’m really drawing blanks on the jump part. I’m not sure if the problem’s just with the placement of the codes or I’m just not seeing it. I hope you guys can help me.

First of all, does the code you have posted compile? I can’t see how it would, since tilt is defined as Vector3 but Quaternion.Euler returns a Quaternion.

I assume that you want to combine the tilt Vector3 with the upward force.

Just add some force on the ball’s rigidbody, and make sure “Use Gravity” is checked, so that the ball falls down.

for example:

    public float thrust;
    public Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
          Vector3 tilt = new Vector3(Input.acceleration.x, 0, -Input.acceleration.z);

          if (Input.GetMouseButtonDown (0)) {
             //jump here
            rb.AddForce((Vector3.up + tilt) * thrust, ForceMode.Impulse); //or any force mode you prefer
          }
    }

If your ball has a rigidbody, and the scene has gravity, there’s a really simple implementation:

int jumpHeight = 10;

if (Input.GetMouseButtonDown(0))
{
    rb.velocity = new Vector3(rb.velocity.x, jumpHeight, rb.velocity.z);
}

You may have to alter it to work by worldspace since I haven’t tested it