- Home /
Jump when touch the screen Android (GAME MADE FOR PC)
Hi guys, someone was so friendly to give me this code for my platform game but i would like to make it for android and what I want is that when someone touches the screen the character jumps, this game is made for pc and i realy don't know how i should change this to work on android
ty in advance!
........................................................CODE.............................................................
var speed: float = 2; // move speed
var jumpSpeed: float = 12; // initial jump speed
var gravity: float = 30;
private var cc: CharacterController;
private var vSpeed: float;
function Update(){
var moveDir = transform.forward * speed; // calculate the horizontal speed
if (!cc) cc = GetComponent(CharacterController); // get the CharacterController
if (cc.isGrounded){ // when grounded...
vSpeed = 0.0; // vSpeed is zero...
if (Input.GetButtonDown("Jump")){ // unless the character jumps
vSpeed = jumpSpeed;
}
}
vSpeed -= gravity*Time.deltaTime; // apply a physically correct gravity
moveDir.y = vSpeed; // add the vertical speed
cc.Move(moveDir*Time.deltaTime); // and finally move the character
}
Answer by starvar · Apr 02, 2012 at 05:49 PM
foreach (Touch touch in Input.touches)
{
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
vSpeed = jumpSpeed;
}
}
So 4 errors: line 14 (foreach...) 2 errors: error 1: expecting ) found 'touch'. error 2: Unexpected token: ).
line 16 (if..) 1error: Unexpected token: if. line 18 (vSpeed..) 1error: expecting :, found '='
so it didn't realy work, but do you you think you could fix those 4 errors?
maybe i showed you a bad example lol. try the things bor walch suggested those should work
Answer by ManiacalSquare · Apr 02, 2012 at 05:50 PM
All you need to change is the (Input.GetButtonDown("Jump")).
You can use Input.GetTouch for this. But You can also use Input.GetMouseButton.
Personally I use the latter for my apps. Seems to work fine on all devices.
Not sure if it also works on iOS. Haven't tested that yet.
Check out the unity script reference for info on these functions.
Answer by ManiacalSquare · Apr 02, 2012 at 05:50 PM
All you need to change is the (Input.GetButtonDown("Jump")).
You can use Input.GetTouch for this. But You can also use Input.GetMouseButton.
Personally I use the latter for my apps. Seems to work fine on all devices.
Not sure if it also works on iOS. Haven't tested that yet.
Check out the unity script reference for info on these functions.
Answer by MD_Reptile · Apr 02, 2012 at 03:23 PM
you need something like this (please check as correct answer if this works for you):
foreach (Touch touch in Input.touches)
{
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
vSpeed = jumpSpeed;
}
}
you would need to stick that code in the place of this one:
if (Input.GetButtonDown("Jump")){ // unless the character jumps
vSpeed = jumpSpeed;
}
PS: what this code does is takes a touch input (i think it works on iOS exactly the same way) anywhere on screen, and then makes whatever code happen inside the if statement.
Hi $$anonymous$$DReprilte,
foreach (Touch touch in Input.touches) --> 2 errors : 1)expecting ) found 'touch'. 2) Unexpected token: ). { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) ---> 1error: 1) unexpected token: if. { vSpeed = jumpSpeed; ---> expecting :,found '=' } }
so it didn't exactly work, do you think you can fix those 4 errors?
$$anonymous$$aybe your using java script, this example is c# sorry
Answer by Lost_C4 · Dec 30, 2012 at 04:30 AM
can we use a simple gui button? or a gui button for easy things like shooting?
People must be voting down your answer comment. You should only answer with solutions.
unrelated/Off topic.
it seems like this should be an entirely different question, but more thought out, or an actual comment to the original question.
Your answer
Follow this Question
Related Questions
How to make a character move towards a side of the screen that's pressed at a constant rate? 2 Answers
How do I rotate an object on one axis to face android touch? 0 Answers
Swerve mechanic, Move left and right with touch 1 Answer
How to Move the Car With Touch? 1 Answer
Android clear touch queue 0 Answers