Jumping via "Apply Root Motion"

Hi everyone,

I’m having a hard time with letting my Player jump into a specific direction. The “jumping” itself works fine, but Force is only applied to the Y Axis.

Anybody know what’s wrong? May that be connected to the Animations?

I’m using this code, wherey Jump() is called from FixedUpdate.

private void Jump()
{
    if (!isInAir && Input.GetKeyDown(KeyCode.Keypad0))
    {
        _animator.applyRootMotion = false;

        isInAir = true;
        _animator.SetBool("Jump", true);
        _animator.SetBool("Grounded", false);

        var gravity = 5;

        var forceZ = _rigidBody.velocity.z > 0 ? 5 : -5;
        var forceX = _rigidBody.velocity.x > 0 ? 5 : -5;

        var force = new Vector3(forceX, gravity, forceZ);
        Debug.Log(force + " RM: " + _animator.applyRootMotion);
        _rigidBody.velocity = Vector3.zero;
        _rigidBody.AddForce(force, ForceMode.VelocityChange);
    }
    else if (isInAir)
    {
        var groundOffset = -0.14f;
        var groundedRadius = 0.5f;
        Vector3 spherePosition = new Vector3(transform.position.x, transform.position.y - groundOffset, transform.position.z);
        var grounded = Physics.CheckSphere(spherePosition, groundedRadius, GroundLayers, QueryTriggerInteraction.Ignore);

        if (grounded)
        {
            _animator.applyRootMotion = true;

            isInAir = false;
            _animator.SetBool("Jump", false);
            _animator.SetBool("Grounded", true);
        }
    }
}

Thanks for help and all the best!

Hi @ArachnidAnimal - was now able to solve it. The error was in the timing. The Physics.CheckSphere was executed too early. The player was still on the ground and isInAir was set to false immediately. I changed the script as follows (MovePlayer() and Jump() are both called from FixedUpdate())

private void MovePlayer()
{
    if (_isInAir)
    {
        return;
    }

    _animator.applyRootMotion = true;

    _animator.SetFloat("InputX", Input.GetAxis("Horizontal"));
    _animator.SetFloat("InputY", Input.GetAxis("Vertical"));
}

private void Jump()
{
    if (!_isInAir && Input.GetKeyDown(KeyCode.Keypad0))
    {
        _isInAir = true;
        _animator.applyRootMotion = false;

        _animator.SetBool("Jump", true);
        _animator.SetBool("Grounded", false);

        var gravity = 5;

        var force = new Vector3(_rigidBody.velocity.x * 1.5f, gravity, _rigidBody.velocity.z * 1.5f);
        _rigidBody.velocity = Vector3.zero;
        _rigidBody.AddForce(force, ForceMode.VelocityChange);
    }
    else if (_isInAir && !_isInMidAir)
    {
        _isInMidAir = transform.position.y > 1;
    }
    else if (_isInMidAir)
    {
        var groundOffset = 0f;
        var groundedRadius = 0.1f;
        Vector3 spherePosition = new Vector3(transform.position.x, transform.position.y - groundOffset, transform.position.z);
        var grounded = Physics.CheckSphere(spherePosition, groundedRadius, GroundLayers, QueryTriggerInteraction.Ignore);

        if (grounded)
        {
            _isInAir = false;
            _isInMidAir = false;
            _animator.SetBool("Jump", false);
            _animator.SetBool("Grounded", true);
        }
    }
}

Have a great day!