Search Unity

Question C# Chracter Controller : Players falls off instantly

Discussion in 'Scripting' started by ajinkyax, Sep 22, 2021.

  1. ajinkyax

    ajinkyax

    Joined:
    Dec 26, 2018
    Posts:
    5
    Hi,

    My player is able to walk and run. But whenever he jumps off edge he falls down instantly like a teleportation, instead of smooth falling.

    // below function is called inside my Update method with Time.deltaTime as delta

    Code (CSharp):
    1. public void CharacterMovement(float delta)
    2.     {
    3.         if (chController.isGrounded && playerVelocity.y < 0)
    4.         {
    5.             playerVelocity.y = 0;
    6.         }
    7.         isSprinting = inputHandler.b_input;
    8.         if (inputHandler.rollFlag) return;
    9.  
    10.         float speed = walkingSpeed;
    11.         if (inputHandler.sprintFlag)
    12.         {
    13.             speed = sprintSpeed;
    14.             isSprinting = true;
    15.         }
    16.  
    17.         Vector2 inputXY = new Vector2(inputHandler.horizontal, inputHandler.vertical);
    18.         moveDirection = GetFollowCameraMovement(inputXY);
    19.         //moveDirection *= delta * speed;
    20.  
    21.         chController.Move(delta * speed * moveDirection);
    22.  
    23.         // rotation
    24.         if (moveDirection != Vector3.zero)
    25.         {
    26.             Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
    27.             targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 * delta);
    28.             chController.transform.rotation = targetRotation;
    29.         }
    30.  
    31.         // Changes the height position of the player..
    32.         //if (jumpAction.triggered && chController.isGrounded)
    33.         //{
    34.         //    playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    35.         //}
    36.  
    37.         // Gravity
    38.         if (chController.isGrounded)
    39.         {
    40.             playerVelocity.y = 0;
    41.         }
    42.         else
    43.         {
    44.             playerVelocity.y += gravityValue;
    45.         }
    46.        
    47.         chController.Move(playerVelocity * delta);
    48.  
    49.  
    50.         //animation
    51.         animatorHandler.UpdateAnimatorValues(inputHandler.moveAmount, 0, isSprinting);
    52.         if (animatorHandler.CanRotate)
    53.         {
    54.             HandleRotation(delta);
    55.         }
    56.     }
     

    Attached Files:

  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    What is 'gravityValue' set to?

    Gravity is usually an accelerative force... which I presume is why you += it when not grounded. But you don't just add the full value... you should scale it by delta.

    Change the this:
    Code (csharp):
    1. playerVelocity.y += gravityValue;
    To this:
    Code (csharp):
    1. playerVelocity.y += gravityValue * delta;
    This way you accumulate a full 'gravityValue' over 1 second of time, not immediately in a single frame. Hence why gravity is commonly described as "units per second per second". Basically you're gaining "units per second" (velocity) "per second". So like 9.8 m/s^2 is saying "every second the velocity increases by 9.8 m/s".

    Note - I'm assuming delta is the delta time since you don't actually use the 'Time' class. We have to guess what all your variables are doing since you're not using any standard variables. So if this doesn't work... well we'll need you to define all these variables for us.
     
    ajinkyax likes this.
  3. ajinkyax

    ajinkyax

    Joined:
    Dec 26, 2018
    Posts:
    5
    fixed it.

    Code (CSharp):
    1. // Gravity
    2.         if (chController.isGrounded && playerVelocity.y < 0)
    3.         {
    4.             playerVelocity.y = 0;
    5.         }
    6.         else
    7.         {
    8.             playerVelocity.y += gravityValue * delta;
    9.         }
    10.  
    11.         chController.Move(playerVelocity * delta);
     
  4. ajinkyax

    ajinkyax

    Joined:
    Dec 26, 2018
    Posts:
    5
    Thanks :)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689