Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by starvar · Apr 02, 2012 at 02:37 PM · androidmovementtouchscreentap

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MD_Reptile · Apr 02, 2012 at 06:41 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
             }
         }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MD_Reptile · Apr 02, 2012 at 03:24 PM 0
Share

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.

avatar image starvar · Apr 02, 2012 at 04:52 PM 0
Share

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?

avatar image MD_Reptile · Dec 30, 2012 at 05:00 AM 0
Share

$$anonymous$$aybe your using java script, this example is c# sorry

avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lost_C4 · Dec 30, 2012 at 02:44 AM 0
Share

Whats wrong with my karma?

avatar image MD_Reptile · Dec 30, 2012 at 04:41 AM 0
Share

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

avatar image gardian06 · Dec 30, 2012 at 05:30 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges