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

Checking if character landed after jumping. [Tutorial]

Discussion in 'Scripting' started by Aspect13, Dec 30, 2018.

  1. Aspect13

    Aspect13

    Joined:
    Sep 29, 2018
    Posts:
    5
    I wanted to do this recently but I could not find any answer on the internet so I managed to do it on my own.
    Basically what I wanted to do is to player the sound when player jumps and lands.
    Also my map is not a flat terrain it's low poly terrain map. Everywhere I looked, I found some weird (at least for me, I'm quite new) or long codes.
    If you need to do this, then here's the tutorial:

    • We create two float variables: groundY and jumpY, we don't need to assign anything to them. We will need these to check if our player really jumped and landed.
    Code (CSharp):
    1.     float groundY;
    2.     float jumpY;
    Now we create an IEnumerator to assign our player's Y position after the jump, so we do this:
    Code (CSharp):
    1.     IEnumerator CheckJumpY()
    2.     {
    3.         yield return new WaitForSeconds(0.8f);
    4.         jumpY = Player.transform.position.y;
    5.     }
    • Why IEnumerator? To check whether the player jumped and landed we need to wait x seconds and then make jumpY, the Y position of our player, so that the our player has time to jump.
    • Okay, now we need to assign our player's Y position before the jump happens. My code looks like this (In Update() of course):
    Code (CSharp):
    1.         if(Input.GetKeyDown(KeyCode.Space) && isGrounded)
    2.         {
    3.             groundY = Player.transform.position.y;
    4.             PlayerSrc.PlayOneShot(PlayerJumpSound); //Not necessary
    5.             Vector3 jump = new Vector3(0f, 2f, 0f);
    6.             rb.AddForce(jump * jumpPower, ForceMode.Impulse);
    7.             StartCoroutine(CheckJumpY());
    8.         }
    • As you can see, before the jump happens we assign our player's Y position to groundY variable and after the jump we start our coroutine which we created earlier and it assign our player's Y position after the jump. Also I added sound effect when player jumps.
    • The final part is that we have to check whether we hit the ground and compare our groundY and jumpY floats. To do this we obviously create onCollisionEnter() function and then we make an If statement like this:
    Code (CSharp):
    1. private void onCollisionEnter(Collision collision)
    2. {
    3.         if(collision.transform.tag == "Ground" && groundY < jumpY)
    4.         {
    5.             PlayerSrc.PlayOneShot(PlayerLandSound); //Not necessary
    6.             groundY = 0f;
    7.             jumpY = 0f;
    8.         }
    9. }
    • So now, if the groundY value is smaller than jumpY, then It's obvious that our player jumped and landed (also here, I play the sound when player lands). Let's say groundY was 0f before the jump and jumpY variable was 3f after we jumped. That means we jumped, then if we collide with the ground, we look "in the past" and compare our positions so that we know whether our player jumped. In the end we reset our variables to 0f so that if we collide with ground without jumping (which happens almost everytime when you have low poly terrain) we won't detect that we jumped and landed.
    • I hope you understood this tutorial, I'm quite bad at explaining :/. Maybe It's not the most precise way to check if we landed after the jump but It's really easy and fast way to do so if you don't want to mess with raycasts which is the way to do this also I think. I wanted to do some sort of tutorial, didn't know where to post it so I decided here. If I you have any tips for me or how to improve the code then feel free to correct me.
     
  2. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    You could also do this ?
    Code (CSharp):
    1.  
    2. if(!cc.isGrounded)
    3. {
    4.     if(!isAirborn) isAirborn = !isAirborn;
    5.     // stuff
    6. }
    7. else
    8. {
    9.     if(isAirborn)
    10.     {
    11.         // Play cool landing sound
    12.         isAirborn = false;
    13.     }
    14. }
     
    Apeles likes this.
  3. Aspect13

    Aspect13

    Joined:
    Sep 29, 2018
    Posts:
    5
    Didn't even know that something like "isGrounded" exists. Just checked in the docs. Yeah that's probably the 100 times simpler version.
     
    Hootlook likes this.
  4. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    Haha its ok we all been there, usually when it gets messy you simply have to change scope :)
     
    Apeles likes this.