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!