Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Continous jumping like in Geometry Dash?

Discussion in 'Scripting' started by Urosq, Feb 12, 2016.

  1. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    Hi guys, i am making a 2D platformer and i need a better jump than the one i have now.

    My character has a rigidbody2D and i am moving it with Add Force. No jitter or anything it is working perfectly.

    Now my jump is implemented using Add Force also and is working good, and i have a bool canJump and OnTriggerEnter2D that checks for Ground and any tag that he can jump off. I have another collider that is Trigger Only on my Player and jump works, he jumps nicely and when he should but here is how i jump.

    I placed a Button and said OnPointerDown and added Jump function to it. Now jump can only work if Bool canJump is true and only being on certain colliders sets it to true.

    But this wont allow me to jump as long as i hold my finger. I would like, like in geometry dash, that when i hold my finger on the screen as soon as he hits the ground and provides a check up if its right tag to fire off and Jump, but he wont and he just jumps one Per input, so in a tight platformer where i have tiny spaces where i require the player to jump continously its just not possible and i cant make it work.

    So if anyone can provide me with an example, or just pointers to maybe a Coroutine to do that for me, i would be so gratefull. Thanks in advance to all who reply! :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    When you check for the jump, don't check for the touch phase Began, but rather check for it being either Moved or Stationary, ie, "still touching."
     
  3. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    I do not know what you mean. I am implementing jump by adding an Event Type to a Button and then selecting Pointer Down, adding my Player to the Object and selecting the Jump function. Is there a better way to implement it ? Thanks for the input!
     
    Last edited: Feb 12, 2016
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Ah, you just need to add an EventTrigger to the Button GameObject, and then you can pick any event you want and hook it up however you want from within that EventTrigger component.
     
  5. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    But i did that and its not working. Maybe its in the code, i will paste it below.

    Code (csharp):
    1.  
    2.  
    3. private bool = canJump;
    4.  
    5. void OnTriggerEnter2D ( Collider2D target) {
    6.  
    7. if ( target.tag == "Ground" || target.tag == "GroundTwo" )
    8.  
    9. canJump = true;
    10.  
    11. } else {
    12. canJump = false;
    13. }
    14.  
    15. public void Jump() {
    16.         if(canJump) {
    17.          
    18.  
    19.             myRigidBody.AddForce (Vector2.up * jumpForce, ForceMode2D.Impulse);
    20.             canJump = false;
    21.          
    22.         }
    23.      
    24.  
    25.     }
    Now this is what i am using. And i tried something right now. Selecting Update Selected from the Event Trigger and Adding Jump to it, made it jump perfect, continous but when i click, he starts jumping and i cant stop the jump, he just keeps jumping non stop. God damn it i feel like the solution is going to be very simple.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    I haven't used that system myself but it seems you might need to track both the press and the release and set your own "JumpButtonIsDown" boolean.

    Then in your update, if "JumpButtonIsDown", and you CanJump, then jump.
     
  7. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    Well i just did that. I added a whole new script that is using Unity.Engine.EventSystems; and also added the OnPointerDown and Up data and set that when pointer is down then i called Jump function from my main PlayerScript and on the UP i set the canJump = false; But that made it worse, it only registers every second or so click, and continous touch is not even working.

    Can you write an example in code, or a snippet of the Event Trigger for the Button?

    I also added a JumpButtonIsDown boolean and i set it to false on Awake, and added a public void to IsJumpButtonDawn and set in it that JumpButtonIsDown = true; and then in Update i added that if its pressed and if canjump is true then jump but its not working. And when it works, the player just flies upward for a huge distance, like 20 jumps distance in a moment.

    Thanks for helping!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    I'm not real bullish on hassling with Unity's UI event system honestly. Despite all the promises, it never quite lives up to expectations.

    However, I have a simple open-sourced button solution here if you would like to take a look at it. This is what I use for all of my games. You can use either a ProximityButton or a VAButton for just one-finger jumping, or modify it however you like:

    https://bitbucket.org/kurtdekker/proximity_buttons

    Edit: Let me clarify the position above a bit: the Unity UI system is fine for UI, but not fine for actual gameplay. It's great for simple tap-tap-tap in the time-wasting portions of a game, i.e., the UI, but when you need control of something, when it is time to actually PLAY the game and you are done pawing at the UI buttons, I definitely prefer to roll my own button input solutions, like what I posted above.
     
    Last edited: Feb 13, 2016
    Urosq likes this.