- Home /
How to add Jump control from touch on screen? ANDROID
hi, so i have this really simple 2D android game with a ball that uses the accelerometer to move from side to side, and i was wondering how i would get the ball to jump with just a touch anywhere on the screen (in Java). Here is my code so far:
#pragma strict
var speed = .2;
private var circ : float;
private var previousPosition : Vector3;
@script RequireComponent(Rigidbody)
function Start()
{
//Find the circumference of the circle so that the circle can be rotated the appropriate amount when rolling
circ = 2 * Mathf.PI * collider.bounds.extents.x;
previousPosition = transform.position;
}
function Update () {
var dir : Vector3 = Vector3.zero;
dir.x = -Input.acceleration.y*.2;
transform.position.x += dir.x;
rigidbody.AddForce(dir * speed * Time.deltaTime);
}
function LateUpdate()
{
var movement : Vector3 = transform.position - previousPosition;
movement = Vector3(movement.z,0, -movement.x);
transform.Rotate(movement / circ * 360, Space.World);
previousPosition = transform.position;
}
Answer by OperationDogBird · Aug 22, 2012 at 12:33 AM
You could add force to the ball using world direction Vector3.up and then multiply by a variable ( * popForce ).
ok, but what would code would make the ball jump when i pressed anywhere on the screen?
The code would be the touch input ( you could use the complicated touch fiasco, or if there is no multi touch use $$anonymous$$ouseButtonDown(0). Once the input happens you can use what i stated above. Thats all the coding you need to do, 1 single line
if(Input.Get$$anonymous$$ouseButtonDown(0)) rigidbody.AddForce(Vector3.up*popForce,Force$$anonymous$$ode.Force);
this does however allow the user to repeatedly tap the screen and the ball will travel higher and higher. you can code in a limit, make a boolean ( inAir ) and only allow the addforce when that is false.
Your answer
Follow this Question
Related Questions
GameObject won't jump with touch 1 Answer
Touch android iput shootting 3 Answers
2D platformer controller script. How to jump with touch? 0 Answers
Accelerometer question 1 Answer
Android issues, jump force not consistent, touch not consistent 0 Answers