x


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

}

more ▼

asked Apr 02 '12 at 02:37 PM

starvar gravatar image

starvar
58 12 17 19

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first
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?

more ▼

answered Apr 02 '12 at 05:49 PM

starvar gravatar image

starvar
58 12 17 19

maybe i showed you a bad example lol. try the things bor walch suggested those should work

Apr 02 '12 at 06:41 PM MD_Reptile
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 02 '12 at 05:50 PM

ManiacalSquare gravatar image

ManiacalSquare
4 2 2 3

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 02 '12 at 05:50 PM

ManiacalSquare gravatar image

ManiacalSquare
4 2 2 3

(comments are locked)
10|3000 characters needed characters left

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;
         }
       }
more ▼

answered Apr 02 '12 at 03:23 PM

MD_Reptile gravatar image

MD_Reptile
299 10 15 19

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.

Apr 02 '12 at 03:24 PM MD_Reptile

Hi MDReprilte,

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?

Apr 02 '12 at 04:52 PM starvar

Maybe your using java script, this example is c# sorry

Dec 30 '12 at 05:00 AM MD_Reptile
(comments are locked)
10|3000 characters needed characters left

can we use a simple gui button? or a gui button for easy things like shooting?

more ▼

answered Dec 30 '12 at 04:30 AM

Lost_C4 gravatar image

Lost_C4
-4 1 2

Whats wrong with my karma?

Dec 30 '12 at 02:44 AM Lost_C4

People must be voting down your answer comment. You should only answer with solutions.

Dec 30 '12 at 04:41 AM MD_Reptile

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.

Dec 30 '12 at 05:30 AM gardian06
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2493
x1374
x583
x488
x39

asked: Apr 02 '12 at 02:37 PM

Seen: 1391 times

Last Updated: Dec 30 '12 at 05:30 AM