Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Using a raycast for jumping

Discussion in 'Scripting' started by ChuckieGreen, May 10, 2018.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    360
    Hi, I am trying to use a raycast to determine if my player can jump or not. This initally works, however, after the first jump my character can continue to jump when in the air.

    The way I believe i had it set up is the raycast determines if it hits something, if it does not, the character can not jump, else the player can jump, but like i said it is not working that way, I am not sure if I maybe have the hit.collider code set wrong.
    If anyone could point out the issue It would be greatly appreciated.

    The function CheckGroundStatus is called in the update function

    Code (CSharp):
    1. void CheckGroundStatus()
    2.     {
    3.  
    4.         RaycastHit hit;
    5.         Ray landingRay = new Ray(transform.position, Vector3.down);
    6.         Debug.DrawRay(transform.position, Vector3.down * DeploymentHeight);
    7.  
    8.         if (Physics.Raycast(landingRay, out hit, DeploymentHeight))
    9.         {
    10.             if (hit.collider == null)
    11.             {
    12.                 canJump = false;
    13.                 Debug.Log(canJump);
    14.             }
    15.             else
    16.             {
    17.                 canJump = true;
    18.                 Debug.Log(canJump);
    19.             }
    20.  
    21.         }
    22.     }
    Jump code

    Code (CSharp):
    1. f (Input.GetKeyDown(KeyCode.Space))
    2.         {
    3.             if (canJump)
    4.             {
    5.                 rb.AddForce(0, forceConst, 0, ForceMode.Impulse);
    6.             }
    7.         }
     
    Last edited: May 10, 2018
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Set canJump to false right after jumping and that will solve your problem.
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Space))
    2. {
    3.     if (canJump)
    4.     {
    5.         canJump = false;
    6.         rb.AddForce(0, forceConst, 0, ForceMode.Impulse);
    7.     }
    8. }
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,202
    Try logging `hit.gameObject.name`. It's possible you're hitting something other than the "ground". Typically with ground-check code like this, you should specify a layerMask to the Raycast, and only include layers that you consider valid for standing on.

    I see you're debug-drawing the ray you're casting. I assume it extends just a little below your character, and that DeploymentHeight isn't too large?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm fairly certain that the bool returned by the raycast, when true, means that the hit.collider is always non-null.
    Therefore, you should move the 'else' statement, so it's "if raycast { inner stuff } else { didn't hit stuff }" :)
     
  5. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    360
    I went through and tested each of your suggestions, to see what would work best.

    Doing this sort of solves the issue, only doing a double jump now :D

    When using debug to test what it is hitting, it doesnt show anything until it actually hits the ground, and yeah the ray just extends slightly below the character.

    Tried this, setting it to if hits terrain (jump) else no jump, did not seem to fix the issue.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I'm glad your issue was resolved. My post was just to point out that, as far as I know, the code inside the if statement regarding the raycast can not have a collider that is null.
     
  7. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    360
    Turns out, it sort of has solved the issue. It seems that canjump is not setting to false quick enough, allowing for me to spam space and do a few more jumps. Going to play around with the code, see if i can sort it out
     
  8. totallynotlace

    totallynotlace

    Joined:
    Oct 19, 2017
    Posts:
    4
    I did a similar code with attacking, I added the toggle for the Bool as the first item in my if statement that checked for keystrokes. Mine appears to not be spammable. Kinda like this:

    Code (csharp):
    1.        if (Input.GetKeyDown (pAttackKey)&& pCanAttack)
    2.         {
    3.         pCanAttack = false;
     
  9. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    360
    Appears I have solved it. Apparently a raycast length of 0.1 was too big and still registering as hitting the terrain (Even though the visual of the raycast was not anywhere near the terrain), shortening it to 0.05 seems to work now though.

    Thanks everyone for the answers :)
     
    Last edited: May 20, 2018