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 DedValve · Mar 01, 2015 at 06:40 PM · touchjumphold

How to jump higher when you hold down with *touch* controls

So I looked through similar questions here on how to jump higher when a button is pressed but since my game is for mobile I need to make it based on when the user taps on the screen and holds it down for a certain period of time.

Here's what I have:

 void Update () {
         // Reset total jumps allowed when not performing a jump and grounded or when on a moving platform.
         if (!jump && (player.grounded || player.IsStuckToPlatform())) {
             jumps = doubleJumping.totalJumps;
         }
 
         //physical button jump
         if (Input.GetButtonDown("Jump")){ //add touch to hold down here?
             StartJump();
         }
 
         //tap jump
         int nbTouches = Input.touchCount;
         
         if(nbTouches > 0)
                     StartJump();
 
     } 
 
 

Void StartJump has the code for the jumping but the actual jumping based on how long a button was pressed is in fixedupdate. If I hold down up or spacebar my character does jump higher but when I tap on the screen the jump is the lowest jump possible as it doesn't detect that I'm constantly holding it down for a higher jump.

Edit: I think I know whats happening, its detecting my touch as a mouse click but doesn't detect when I hold it longer as I would a button. I can't find anything regarding holding down the touchscreen. OnMouseButtonDown doesn't work as it for some weird reason gives me an error.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Mar 01, 2015 at 06:43 PM

You need to examine the touchPhase of each touch on the screen: http://docs.unity3d.com/ScriptReference/TouchPhase.html

In the frame in which the touch is first registered, this will be "Began". In every subsequent frame the touch is held, it will either be "Stationary " or "Moved", until it is ended or cancelled. So you can consider those two states as equivalent to a mouse or key button being held.

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
0

Answer by DedValve · Mar 01, 2015 at 09:32 PM

I tried that but I can't wrap my head around how I'm suppose to put it in this code. This isn't code I wrote btw, I'm trying to build on top of it and make mobile touch controls.

This is what I have in update:

 //physical button jump
         if (Input.GetButtonDown("Jump")){ //add touch to hold down here?
             StartJump();
         }
 
         if (Input.GetTouch(0).phase == TouchPhase.Began){
             StartJump();
         }

This is Fixed Update which runs how high the player goes based on how long the button was pressed:

 // If you need to hold the Jump input to jump higher...
             if (jumpType == JumpType.HoldToJumpHigher) {
                 // When there is an initial jump...
                 if (initialJump) {
                     // ... set the y velocity to the player's initial jump value.
                     float yVel = jumpFactor * (doubleJump ? holdToJumpHigher.initialDoubleJump : holdToJumpHigher.initialJump);


And this is the start jump function:

 void StartJump(){ 
     // If the jump button is pressed, jumps are allowed and the player is not dashing, sliding, on a ladder or crouching under an obstacle...
     if (!jump && jumps > 0 && !player.dashing && !player.sliding && !player.onLadder && (!player.crouching || (player.crouching && player.AllowedToStandUp()))) {
         // If the player is grounded...
         if (player.grounded) {
             // ... initialize jump.
             InitJump();
             // If the player is not grounded and totalJumps is higher than 1...
         } else if(doubleJumping.totalJumps > 1) {
             // ... initialize jump if the Y velocity is inside the double jump window (or when there isn't a window).
             if (!doubleJumping.jumpWindow || (doubleJumping.jumpWindow && rigidbody2D.velocity.y > doubleJumping.jumpWindowMin && rigidbody2D.velocity.y < doubleJumping.jumpWindowMax)) {
                 doubleJump = true;
                 InitJump();
             }
         }
     }
 }


I think what I have to do is figure out how to make the fixedupdate function into its own seperate function then in update call "if touchphase.stationary call holdtojumphigher();"

Problem is that the variable created in that fixedupdate is used in the rest of fixed update.

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make a 2D mobile tap jump with small jump and if I Hold a touch it would jump higher 0 Answers

Jump and Hold on!! 0 Answers

How to make only one ball bounce? 0 Answers

Swipe and Hold to move character . 1 Answer

Touch the screen and jump two time with the same height 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