Search Unity

Jumping physics gone wrong

Discussion in 'Scripting' started by KitoCode, Jun 29, 2019.

  1. KitoCode

    KitoCode

    Joined:
    Jan 10, 2015
    Posts:
    11
    Good day. I am having issues with trying to understand how i can build better jumping and gravity physics in my game.. And please don't suggest rigidbody :p

    I have two scripts. One that in Update called to update the controller and then right after I update the motor of the controller.

    Code (CSharp):
    1.  
    2. // Update is called once per frame
    3. public void Update()
    4. {
    5.     motor.VerticalVelocity = motor.MoveVector.y;
    6.     motor.MoveVector = Vector3.zero;
    7.  
    8.     //Apply any changes we need done to the Motor, then update them.
    9.     UpdateController();
    10.     motor.UpdateMotor();
    11.  
    12.     //animator.Update();
    13. }
    14.  

    Below is the player controller, this is where the jumping logic comes in.

    Code (CSharp):
    1.  
    2. private override void UpdateController()
    3. {  
    4.     bool isGrounded = this.GetComponent<CharacterController>().isGrounded;
    5.        
    6.     //we check if isGrounded is true and we pressed Space button
    7.     if (isGrounded == true && Input.GetKey(KeyBindings.Jump))
    8.     {
    9.         isJumping = true;          
    10.         base.motor.VerticalVelocity = minJumpForce;
    11.     }
    12.  
    13.     if (isJumping && Input.GetKeyUp(KeyBindings.Jump))
    14.         isJumping = false;
    15.  
    16.     if(isJumping && isGrounded == false && Input.GetKey(KeyBindings.Jump))
    17.     {
    18.         base.motor.VerticalVelocity += jumpForce;
    19.  
    20.         if (base.motor.VerticalVelocity >= maxJumpForce)
    21.         {
    22.             base.motor.VerticalVelocity = maxJumpForce;
    23.             isJumping = false;
    24.         }
    25.     }
    26.  
    27.     if (Input.GetKey(KeyBindings.Left) || Input.GetKey(KeyCode.LeftArrow))
    28.         base.motor.MoveVector += new Vector3(-1, 0, 0);
    29.     else if (Input.GetKey(KeyBindings.Right) || Input.GetKey(KeyCode.RightArrow))
    30.         base.motor.MoveVector += new Vector3(1, 0, 0);
    31.  


    Below we update the motor.

    Code (CSharp):
    1.  
    2. public void UpdateMotor()
    3. {
    4.     // Transform MoveVector to World Space
    5.     MoveVector = transform.TransformDirection(MoveVector);
    6.  
    7.     // Normalize our MoveVector if Magnitude is > 1
    8.     if (MoveVector.magnitude > 1)
    9.         MoveVector = Vector3.Normalize(MoveVector);
    10.  
    11.     // Multiply MoveVector by MoveSpeed
    12.     MoveVector *= MoveSpeed;
    13.  
    14.     // Reapply Verticle Velocity to MoveVector.y
    15.     MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
    16.  
    17.     // Apply Gravity
    18.     ApplyGravity();
    19.  
    20.     // Move the Character in World Space
    21.     GetComponent<CharacterController>().Move((MoveVector * 5) * Time.deltaTime);
    22. }
    23.    
    24. /// <summary>
    25. /// Applies the gravity.
    26. /// </summary>
    27. private void ApplyGravity()
    28. {
    29.     //Make sure we are not exceeding out -Terminal Velocity
    30.     if (MoveVector.y > -TerminalVelocity)
    31.         MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
    32.  
    33.     if (GetComponent<CharacterController>().isGrounded && MoveVector.y < -1.0f)
    34.         MoveVector = new Vector3(MoveVector.x, -1.0f, MoveVector.z);
    35. }
    36.  

    I would appreciate any help.. The jumping doesn't seem to be consistent :( If someone wants to talk in person, id love to talk out my issues and show more code.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The core issue is that you're treating your entire character's movement as one vector, which you reset during input, so you're clipping all of your possible extra forces like jump impulse or gravity accretion. You should keep a separate velocity vector that you add your input movement to.
     
    palex-nx likes this.
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Moreover, you should keep your movement separated by axes because sometimes (quite often) horizontal and vertical movement logic in games differ