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;
}
I'd refactor a little bit like this:
using UnityEngine;
public class Player : MonoBehaviour
{
public Rigidbody playerBody;
public Animator playerAnimator;
public float jumpForce = 5f;
public float movementSpeed = 10f;
private bool isGrounded = false;
private bool hasPlayerJumped = false;
private bool isPlayerMoving = false;
private Vector2 playerMovementInput;
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;
isPlayerMoving = true;
}
private void CancelMovement()
{
playerMovementInput = Vector2.zero;
isPlayerMoving = false;
}
private void SetJump()
{
// Jump
TryToJump(isGrounded);
}
void FixedUpdate()
{
TryMove(isGrounded);
}
public bool TryMove(bool isPlayerGrounded)
{
if (hasPlayerJumped)
{
return false;
}
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);
return true;
}
private void TryToJump(bool isPlayerGrounded)
{
if (isPlayerGrounded && !hasPlayerJumped)
{
playerBody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
playerAnimator.SetBool("hasJumped", true);
hasPlayerJumped = true;
}
}
}
I haven't tested it yet, since I'd need to create the new Unity InputSystem along with Controls and all the variables you mention.
Also note that isGrounded
variable must be set using the collider (or Rigidbody) when it touches the ground.
Let me know if you find any issues.