Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Character jumps super high when against a wall.

Discussion in 'Scripting' started by Deleted User, Sep 3, 2020.

  1. Deleted User

    Deleted User

    Guest

    I don't know if it is because of friction? When my player is against pretty much any object with a collider and then when I jump it jumps so high. I've tried using raycast to ground and that is not helping, also tried the normal controller.IsGrounded and not working either.

    So I guess I need to do something with colliders?

    Hope this is the right forum.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Your ground check is likely considering the wall to be the ground. Also you might be using GetKey instead of GetKeyDown?
     
    Dextozz likes this.
  3. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1.  private void Jumping()
    2.     {
    3.         RayToGround();
    4.  
    5.         if (Input.GetKey(controls.PlayerJumpButton) && playerOnGround)
    6.         {
    7.             playerVelocityY.y += Mathf.Sqrt(jumpForce * -2.0f * gravity);
    8.             currentSpeed = 2f;
    9.         }
    10.  
    11.         playerVelocityY.y += gravity * Time.deltaTime;
    12.         controller.Move(playerVelocityY * Time.deltaTime);
    13.     }
    14.  
    15.     RaycastHit hit;
    16.     public GameObject rayOrigin;
    17.  
    18.     private void RayToGround()
    19.     {
    20.         if(Physics.Raycast(rayOrigin.transform.position, Vector3.down, out hit, rayDistance, ground))
    21.             Debug.DrawRay(rayOrigin.transform.position, Vector3.down * rayDistance, Color.red);
    22.  
    23.         if(hit.collider != null)
    24.         {
    25.             playerOnGround = true;
    26.             currentSpeed = 5f;
    27.         }
    28.          
    29.         else
    30.             playerOnGround = false;
    31.  
    32.     }
    here's my code.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Your RayToGround code is the main issue here. Only the Debug.Log call is inside the if statement for the raycast. If the raycast doesn't hit, the RaycastHit won't get overwritten. So on line 23 you're testing an old RaycastHit. Basically once you're grounded once, you'll be grounded forever.

    You need to do something like this:

    Code (CSharp):
    1.     private void RayToGround()
    2.     {
    3.         if(Physics.Raycast(rayOrigin.transform.position, Vector3.down, out hit, rayDistance, ground)) {
    4.             Debug.DrawRay(rayOrigin.transform.position, Vector3.down * rayDistance, Color.red);
    5.             playerOnGround = true;
    6.             currentSpeed = 5f;
    7.         }
    8.        
    9.         else {
    10.             playerOnGround = false;
    11.         }
    12.     }
     
  5. Deleted User

    Deleted User

    Guest

    It didn't fix my issue, still if I go against a wall the player still jumps super high. I think something else is the problem within the project itself?
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Debug it. Print out each frame whether you're grounded or not. If you're still grounded in the air, it's a bug in the code or how your character is set up.
     
  7. Deleted User

    Deleted User

    Guest

    Hey again,
    I did what you said. Saw no problems.
    But I did something else that acutally fixed it, I just increased th step offset to 1 instead of 0.3. In the CharacterController. :) So now the bug is not there anymore. Thanks for your time! :)