Unity new input system is not always working in some situations.
I was trying to make my player jump but I got an Issue, when my player is walking in the direction W+A my player does not jump, also when my player is running in the directions W, W+A, W+D.
I think it has something to do with new input system, something that I must change or I don't know because.
In the situations I mentioned above it's not debugging the message
public void SetJump(InputAction.CallbackContext value)//usa o parametro pra recer resultado das action (nessa caso vetor2
{
if (value.started) Debug.Log("I'm Ready to jump");
Note: my player is jumping in other situations just not on the situations I mentioned above.
Note: my method IsGrounded
is also perfectly functioning I checked it, I even put my player on flat terrain.
Now, I will show my entire code, and how I configured my input system
Input system configuration:
Code:
public class PlayerMovement : MonoBehaviour
{
[Header("Config Player")]
[SerializeField] private float speed = 3f;
[SerializeField] private float speedWhileRunningMultiplier = 1.3f;
[SerializeField] private float gravity = -9.8f;
[SerializeField] private float gravityMultiplier = 3f;
[SerializeField] private float jumpForce = 9.8f;
[SerializeField] private float smoothTime = 0.05f;
[SerializeField] private bool isGrounded = false;
private CharacterController controller;
private Animator anim;
private bool isWalking, isRunning = false;//for animation purposes
private float currentSpeedRotating;
private float verticalVelocity = 0f;
private Vector2 movement2D;
private Vector3 direction;
private bool IsGrounded() => controller.isGrounded;
private void Awake()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
void FixedUpdate()
{
ApplyMovement();
ApplyRotation();
ApplyGravity();
}
public void SetMovement(InputAction.CallbackContext value)
{
movement2D = value.ReadValue<Vector2>();
direction = new Vector3(movement2D.x, 0, movement2D.y);
direction.Normalize();
}
public void SetJump(InputAction.CallbackContext value)
{
if (value.started) Debug.Log("I'm Ready to jump");
if (!value.started) return;
if (!IsGrounded()) return;
verticalVelocity += jumpForce;
}
public void SetRun(InputAction.CallbackContext value)
{
isRunning = value.performed ? true : false;
}
void ApplyMovement()
{
isWalking = direction.x != 0 || direction.z != 0;
float currentSpeed = speed * (isRunning ? speedWhileRunningMultiplier : 1f);
controller.Move(direction * currentSpeed * Time.deltaTime);
anim.SetBool("isWalking", isWalking);
anim.SetBool("isRunning", isRunning);
}
void ApplyRotation()
{
if (movement2D.sqrMagnitude == 0) return;
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref currentSpeedRotating, smoothTime);
transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);
}
void ApplyGravity()
{
if (IsGrounded() && verticalVelocity <0.0f)
{
verticalVelocity = -1f;
}
else
{
verticalVelocity += gravity * gravityMultiplier * Time.deltaTime;
}
direction.y = verticalVelocity;
}
}
My player has charactercontroller component, capsule collider, Player Input System attached to it and the methods properly linked to the player
After testing it and modying my code, I've figured there was no issue with it, it was actually my keyboard, for some reason it's not working all the keys in the situation I mentioned above, so after I changed my keyboard my code worked