- Home /
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.
Answer by pako · Oct 31, 2017 at 11:21 AM
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
}
}
Answer by AidanHorton · Oct 31, 2017 at 12:21 PM
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
Your answer
Follow this Question
Related Questions
Making player object jump other than tapping the screen in android phones 1 Answer
How to make roll a ball android compatible 1 Answer
how to make ball rolling forward while follows a curve track? 0 Answers
Tilt control for jump 0 Answers
Problem with touch button on android platformer game. 0 Answers