Search Unity

Question [SOLVED] Wall Run Fall Speed

Discussion in 'Scripting' started by Eisenwelle, Apr 9, 2021.

  1. Eisenwelle

    Eisenwelle

    Joined:
    Mar 18, 2021
    Posts:
    22
    In my player controller, I have code for wall running and jumping. It 100% works as intended, but the player falls down the wall a bit faster than I'd prefer, making the wall run duration overly short. I'd like to reduce the speed at which the player falls, preferably by adding an upward force to the player while the player is wall running, but I'm not sure where/how to change this, as I used a pre made script as a template, and I'm still pretty new to Unity. I do have a float gravity that is used to act as a rigidbodys gravity would (I'm using character controller so the player doesn't have a rigidbody to apply normal gravity to), but it's called in a different script, and I have no idea how to call enums or floats from other scripts. Here is the relevant section of script:
    Code (CSharp):
    1.      /**************************** WALLRUNNING ****************************/
    2.     void WallrunningMovement()
    3.     {
    4.         Vector3 input = playerInput.input;
    5.         float s = (input.y > 0) ? input.y : 0;
    6.  
    7.         Vector3 move = wallNormal * s;
    8.  
    9.         if (playerInput.Jump())
    10.         {
    11.             movement.Jump(((Vector3.up * (s + 0.5f)) + (wallNormal * 2f * s) + (transform.right * -wallDir * 1.25f)).normalized, s + 0.5f);
    12.             playerInput.ResetJump();
    13.             status = Status.moving;
    14.         }
    15.  
    16.         if (!hasWallToSide(wallDir) || movement.grounded)
    17.             status = Status.moving;
    18.  
    19.         movement.Move(move, movement.runSpeed, (1f - s) + (s / 4f));
    20.     }
    21.  
    22.     void CheckForWallrun()
    23.     {
    24.         if (!canInteract || movement.grounded || movement.moveDirection.y >= 0)
    25.             return;
    26.  
    27.         int wall = 0;
    28.         if (hasWallToSide(1))
    29.             wall = 1;
    30.         else if (hasWallToSide(-1))
    31.             wall = -1;
    32.  
    33.         if (wall == 0) return;
    34.  
    35.         if(Physics.Raycast(transform.position + (transform.right * wall * radius), transform.right * wall, out var hit, halfradius, wallrunLayer))
    36.         {
    37.             wallDir = wall;
    38.             wallNormal = Vector3.Cross(hit.normal, Vector3.up) * -wallDir;
    39.             status = Status.wallRunning;
    40.         }
    41.     }
    42.  
    43.     bool hasWallToSide(int dir)
    44.     {
    45.         //Check for ladder in front of player
    46.         Vector3 top = transform.position + (transform.right * 0.25f * dir);
    47.         Vector3 bottom = top - (transform.up * radius);
    48.         top += (transform.up * radius);
    49.  
    50.         return (Physics.CapsuleCastAll(top, bottom, 0.25f, transform.right * dir, 0.05f, wallrunLayer).Length >= 1);
    51.     }
    52.     /*********************************************************************/
    Any help is greatly appreciated, and I'll answer any questions asap!
     
    Last edited: Apr 9, 2021
  2. Eisenwelle

    Eisenwelle

    Joined:
    Mar 18, 2021
    Posts:
    22
    I ended up solving this by running a check to see if the character controller was in contact with a wall layer object, and changed the "gravity" accordingly.