Search Unity

Custom controller not able to jump and find ground

Discussion in 'Scripting' started by thestrandedmoose, Mar 8, 2019.

  1. thestrandedmoose

    thestrandedmoose

    Joined:
    Jul 21, 2015
    Posts:
    70
    Hey guys- I created my own kinematic character controller which can move, snap to the ground, and jump smoothly. However, in order to get the jumping smooth, I needed to unground the character and apply a Sin lerp.

    As a result, if a character jumps and intercepts a slope, he continues jumping through the slope to the ground (since he is ungrounded at that point). Does anyone know how I can get my Gravity/grounding method and my jump method working together?

    Code (CSharp):
    1.   private void FixedUpdate()
    2.     {
    3.         Movement();
    4.  
    5.         Gravity();
    6.  
    7.         Jump();
    8.  
    9.         FinalMovement();
    10.    
    11.     }
    12.  
    13.     private void Gravity()
    14.     {
    15.         boxSize = Vector3.one * .4f;
    16.         boxPos = new Vector3(transform.position.x, transform.position.y - transform.localScale.y, transform.position.z);
    17.  
    18.         isGrounded = Physics.CheckBox(boxPos, boxSize, Quaternion.identity, excludePlayer);
    19.  
    20.         if (!isJumping)
    21.         {
    22.             if (isGrounded)
    23.             {
    24.                 currentGravity = 0;
    25.  
    26.                 Ray ray = new Ray(transform.position, Vector3.down * 2);
    27.                 RaycastHit hit;
    28.  
    29.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity, excludePlayer))
    30.                 {
    31.                     if (hit.distance < 2)
    32.                     {
    33.                         Vector3 groundSnapPosition = new Vector3(transform.position.x, hit.point.y + transform.localScale.y, transform.position.z);
    34.                         transform.position = groundSnapPosition;
    35.                     }
    36.                     else
    37.                     {
    38.                         currentGravity = -gravity;
    39.                     }
    40.                 }
    41.             }
    42.             else
    43.             {
    44.                 currentGravity = -gravity;
    45.             }
    46.         }
    47.     }
    48.  
    49.     private void Jump()
    50.     {
    51.         //reset when we press spacebar
    52.         if (jumpRequested && isGrounded)
    53.         {
    54.             unGround = true;
    55.             jumpStartPos = 0;
    56.             jumpEndPos = jumpDistance;
    57.             currentLerpTime = 0f;
    58.             jumpRequested = false;
    59.             isJumping = true;
    60.         }
    61.         else
    62.         {
    63.             jumpRequested = false;
    64.         }
    65.  
    66.         if (isJumping)
    67.         {
    68.             //increment timer once per frame
    69.             currentLerpTime += Time.deltaTime;
    70.             if (currentLerpTime > lerpTime)
    71.             {
    72.                 currentLerpTime = lerpTime;
    73.                 isJumping = false;
    74.             }
    75.  
    76.             //lerp!
    77.             float perc = currentLerpTime / lerpTime;
    78.             perc = Mathf.Sin(perc * Mathf.PI);
    79.  
    80.             prevJumpHeight = jumpHeight;
    81.             jumpHeight = Mathf.Lerp(jumpStartPos, jumpEndPos, perc);
    82.             jumpDelta = jumpHeight - prevJumpHeight;
    83.  
    84.         }
    85.     }
    86.     private void FinalMovement()
    87.     {
    88.         Vector3 movement = new Vector3(transform.position.x, transform.position.y + jumpDelta - currentGravity, transform.position.z);
    89.         transform.position = movement;
    90.     }
     
    Last edited: Mar 8, 2019