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 HunterKrech · Aug 22, 2012 at 12:14 AM · androidtouchjumpaccelerometer

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

1 Reply

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

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 ).

Comment
Add comment · Show 2 · 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 HunterKrech · Aug 22, 2012 at 01:02 AM 0
Share

ok, but what would code would make the ball jump when i pressed anywhere on the screen?

avatar image OperationDogBird · Aug 22, 2012 at 02:38 AM 0
Share

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

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

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


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