I would like to ask you for help.
I am using the new Unity Input system and I am having a trouble to make jump working.
I need to reset an animation of jumping back to idle/walking as well as set hasPlayerJumped variable to false.
Would any of you have any ideas?
Thank you in advance.
The TryMove(...) is called from FixedUpdate().
public bool TryMove(bool isPlayerGrounded)
{
Vector3 _moveVector = transform.TransformDirection(new Vector3(playerMovementInput.x, 0f, playerMovementInput.y)) * movementSpeed;
playerBody.velocity = new Vector3(_moveVector.x, playerBody.velocity.y, _moveVector.z);
playerAnimator.SetFloat("forwardSpeed", playerBody.velocity.normalized.magnitude);
// Jump
TryToJump(isPlayerGrounded);
return true;
}
private void TryToJump(bool isPlayerGrounded)
{
if (isPlayerGrounded && hasPlayerJumped)
{
playerBody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
playerAnimator.SetBool("hasJumped", true);
}
}
Here is the code for the new Input System.
private void Start()
{
InitControlsCallbacks();
}
private void InitControlsCallbacks()
{
Controls.Player.Move.performed += ctx => StartMoving(ctx.ReadValue<Vector2>());
Controls.Player.Move.canceled += ctx => CancelMovement();
Controls.Player.Jump.performed += ctx => SetJump();
}
private void StartMoving(Vector2 _movementVector)
{
playerMovementInput = _movementVector;
isPlayerMooving = true;
}
private void CancelMovement()
{
playerMovementInput = Vector2.zero;
isPlayerMooving = false;
}
private void SetJump()
{
hasPlayerJumped = true;
}