Search Unity

Question Jump Rigidbody while moving

Discussion in 'Scripting' started by Digitex1, Nov 27, 2022.

  1. Digitex1

    Digitex1

    Joined:
    Aug 28, 2017
    Posts:
    42
    Hello,

    I'm making a basic FPS controller, I do make the player move using the following code inside a Move() method called regularly from FixedUpdate():



    Then, I also have this Jump() method called from FixedUpdate before the Move() method:



    the code works correctly when the player isn't moving.

    However, when the player is moving forward: it's just not working - the rigidbody barely make any movements.

    I do not understand why. what am I missing?
     
    Last edited: Nov 28, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    That is a lot of logic to move a Rigidbody... looks like you are mixing a PD controller for horizontal motion with some type of transient timed force function for vertical jumping.

    I'm not even sure how I would begin to understand such a setup, except perhaps to instrument it and feed it known inputs and study what it does with those known inputs.

    All you really need is to drive X and Z velocity (WITHOUT touching the Y velocity!) based upon the move controls, then set the Y velocity (WITHOUT touching X,Z velocity!) based upon jump controls and let the physics system do everything else.

    If you want gradual stop / start, just do the velocity target tweening yourself to mimic the PD controller's feel. I like to use this pattern:

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     
    Last edited: Nov 27, 2022