Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Top Down Jump. (SOLVED)

Discussion in '2D' started by lothlorientos, Jun 19, 2014.

  1. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    Hi. I'm working on a top down 2D game in which I want the player to be able to jump for a certain duration on click.
    I tried a lot different ways but still I'm not sure how to do this...

    What I thought to do is to just set a bool "jumping" to true when player clicks (BUT doesn't matter how much the player holds the click key down , it should only jump only once and not being able to jump again while it is still jumping) but I'm not sure how to do this nad how to set the time that the jumping will be true.
     
  2. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    You could do it with Booleans. When the button is clicked it sets activateJump boolean to true.

    Code (JavaScript):
    1. if (activateJump = false && <touch code for jump button>)
    2. {
    3.     activateJump = true;
    4.     do jump code
    5. }
    6.  
    and in your ground check code set activateJump = false;
     
  3. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    Yes but the thing is that the game is top down so I can't control the jump with the ground check
    because when the player jumps it only plays an animation that makes the illusion of jumping.
    So what I need is something like a timer that will set the boolean from false to true for a certain amount of time
    but also it won't let you jump again while already "jumping".
    Also if you know the code in C# it would be more helpful since I'm working with C#. If not, it's ok I just need the logic.
     
  4. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    Even though it is 2d... it is still in a 3d world.. cant you just jump on the Z axis?
     
  5. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    yes that is possible but why would i do that? I just need an animation to play to give the illusion of jumping.
    I don't think there is any gravity on Z axis anyway... So I don't think that would be possible.
    And even if it is possible I think it's better with a timer because as the level progresses I want the jump time to become
    smaller every second.
     
  6. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    Then you would simply have a setbool on the animation system. so when you set the bool to true to play the animation you check if that bool is true with the control. Then you would have to alter the animation to shorten the animation play time...

    Im sorry without actually workin on the project its hard for me to explain and work with all the possible outcomes :(
     
  7. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    Very interesting! But how can I alter the animation to shorten it?
     
  8. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    I understand your logic but I'm not sure how to shorten the animation play time
     
  9. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    i have never done it. but it would entail altering the animations sample to a higher number i would beleive. It's something you would have to dig in and play with. This is all theoreetical as i said ive never messed with changing that stuff.
     
  10. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    Ok thank you very much for the info ;)
    I'll try my best :)
     
  11. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    The issue with a timer is when you reduce the timer the animation is going to still play the same length. So you have to work those two together.
     
  12. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    Ye indeed... and another think I'm thinking is that if I shorten the animation it might mess up the frames if I actually have to lower frame rate
     
  13. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    if you leave the animation times the same and the jump times always the same you could make it so when the jump button pressed the animation bool get set to true anim.SetBool("Jumping" , true); then you set up a timer for the same length of time and anim.SetBool("Jumping" , false); then that will open up the jump button again.

    as i said somet rial and error is required.
     
  14. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    Ye, that's what I tried to do at first but I'm not sure how to do it.
    I have been working with C# for like 6 months now on Visual Studio but I'm new to the Unity C# and there are some differences. I'm trying to find a timer to do that but I can't find any on Unity C#.
    On Visual Studio I would use a time interval and I would be all set up, but here I'm not sure how it's done :/
     
  15. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    Something like....

    Code (JavaScript):
    1. var jumpTimer : float = 0.0f;
    2. var jumpTime : float = 2.0f;
    3. var isJumping : boolean = false;
    4.  
    5. function Update()
    6. {
    7.     <button control stuff that sets isJumping to true or however you wanna do it.>
    8.     if (isJumping)
    9.     {
    10.         jumpTimer += Time.deltaTime;
    11.  
    12.         if (jumpTimer >= jumpTime)
    13.         {
    14.             jumpTimer = 0.0f;
    15.             isJumping = false;
    16.             <set your jump stuff to false>
    17.         }
    18.     }
    19. }
    sorry for Javascript i dont use much c# but it should be easily converted to c#
     
    Last edited: Jun 19, 2014
    lothlorientos likes this.
  16. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    It works like a charm!!!!!!!!! :D
    Thank you very much my friend!! ;)
     
  17. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    any time.
     
    lothlorientos likes this.
  18. ShibaWaifu

    ShibaWaifu

    Joined:
    Apr 17, 2018
    Posts:
    12
    Hi ! can you share your jumping code ?