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 Dr_GeoFry · Apr 13, 2012 at 06:57 AM · androidjumping

Jumping irregularities

I have a script that makes a ball jump when the user taps the screen. The problem I have now is that if I tap the screen, everything works fine, but if I hold my finger on the screen the ball continues to rise as long as I hold my finger. How can I limit that? Also, how can I limit the total number of taps to, say two? Additionally, (sorry for this many questions) I have two codes that make sense to me, but only one works. I'm looking for some code clarity.

Why does

if (fingerCount > 0) dir.y = -Input.acceleration.x*.5; transform.position.y += dir.y; }

work, but

if (fingerCount > 0) rigidbody.AddRelativeForce (Vector3.up * jumpStrength); }

does not work?

Thanks for your assistance and getting through this whole thing.

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
1
Best Answer

Answer by zazery · Apr 13, 2012 at 08:32 AM

The reason why the ball continues to rise when you touch the screen is the condition for your if-statement will always be true since Input.touchCount will be > 0. Instead you want to perform the jump code only the first time you first touch the screen.

The best way to do this is check if the finger's phase is TouchPhase.Began. It looks like you're using Javascript so I wrote the example in it.

 function Update () 
 {
     var fingerCount : int = 0;
     
     for (var touch : Touch in Input.touches)
     {
         if (touch.phase == TouchPhase.Began)
             fingerCount++;
     }
     
     // Your code here
 }

Alternatively, a more naive approach but perhaps easier to understand is to use a boolean that is reset when the user stops touching the screen.

 var firstTouch : boolean = true;

 function Update()
 {
     if (firstTouch && Input.touchCount > 0)
     {
         firstTouch = false;
         // your jump code here without the if-statement
     }

     if (Input.touchCount == 0)
         firstTouch = true;
 }

There are many possible reasons for why rigidbody.AddRelativeForce is not working. Check to see if you have a rigidbody attached to the sphere and isKinematic is unchecked. Make sure jumpStrength is large enough. I made it work by creating a default sphere with a rigidbody and jumpStrength set to 100.

Your best bet is to use rigidbody since changing transform.position does not implement physics or more importantly handle collision detection. An alternative approach is to use a CharacterController if you want more control than the physics engine provides but you will have to write the code to keep track of and compute velocity and acceleration. Read more about it in the Unity manual.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump Script for 2D (JavaScript) 0 Answers

Jumping with touch input. 0 Answers

Jump heights are different on different devices 0 Answers

Moving forward whilst jumping 3 Answers

Physics on a Ball 1 Answer


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