Need help with jumping mechanics for rigidbody rootmotion controller

Hey all,

so I’ve been working on my own custom third person character controller using a rigidbody and root motion. I’ve got all the movement and drawing sword mechanics figured out already and working the way I want it to. The part I’m having issues with is getting the jump mechanism to work. I’ve got 3 separate parts of the jump animation (jump start, in air and landing) and none of those have root motion. When I apply addforce to my rigidbody, during the in air animation the character only goes up and down in the Y axis but ignores the velocity in the horizontal direction. What I am trying to do is to go from a running animation into a jump animation and be able to adjust the height of the jump kind of like the third person controller in the starter assets

I’ve come to the conclusion that I need to apply a velocity based on the input and velocity of the rigidbody before the jump to the inplace animations. I have code that checks whether the character is grounded or not but what I don’t know is how to script a velocity based on the input onto the jump animations while the rigidbody is not grounded.

I’m using onAnimatorMove to apply the root motion through code to the other locomotion animations so I’m wondering if I need to code something in there to apply a velocity/rootmotion to the jump animations. Essentially what I need is a bit of extra code to apply the horizontal motion/velocity to the character while it is not grounded. Any help would be appreciated on how I would code something like that.

    private void OnAnimatorMove()
    {
        m_animator.ApplyBuiltinRootMotion();

        if (Grounded && Time.deltaTime > 0)
        {
            Vector3 v = (m_animator.deltaPosition * 1) / Time.deltaTime;

            v.y = m_rigidbody.velocity.y;
            m_rigidbody.velocity = v;

            if (isSpaceKeyPressed)
            {
                m_rigidbody.AddForce(Vector3.up * _verticalVelocity, ForceMode.Impulse);
            }

        }
    }

I have actually figured it out now. For anyone that comes across the same issue here is what I did. With the code below I simply calculated the rigidbody movement via code but only when the character is not grounded during the jump animations. I’m using the new input system so I stored my horizontal and vertical input axis in moveDir and multiplied it by the camera forward and right then used rigidbody.moveposition as shown. If anyone has any questions feel free to let me know.

        if(!Grounded)
        {
            Vector3 moveForward = m_Camera.forward * moveDir.y;
            Vector3 moveRight = m_Camera.right * moveDir.x;
            Vector3 m_input = (moveForward + moveRight).normalized;

            m_rigidbody.MovePosition(transform.position + m_input * Time.deltaTime * 4f);
        }
        else
        {
            return;
        }
1 Like