- Home /
Something weird with my input system
Ok. I followed this tutorial for 3d movement in unity. Here is the code for movement: using UnityEngine;
[SerializeField] CharacterController controller;
[SerializeField] float speed = 15f;
Vector2 horizontalInput;
[SerializeField] float jumpHeight = 3.5f;
bool jump;
[SerializeField] float gravity = -30f;
Vector3 verticalVelocity = Vector3.zero;
[SerializeField] LayerMask groundMask;
bool isGrounded;
private void Update()
{
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundMask);
if (isGrounded)
{
verticalVelocity.y = 0;
}
Vector3 horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * speed;
controller.Move(horizontalVelocity * Time.deltaTime);
// Jump: v = sqrt(-2 * jumpHeight * gravity)
if (jump)
{
if(isGrounded)
{
verticalVelocity.y = Mathf.Sqrt(-2f * jumpHeight * gravity);
}
jump = false;
}
verticalVelocity.y += gravity * Time.deltaTime;
controller.Move(verticalVelocity * Time.deltaTime);
}
public void RecieveInput (Vector2 _horizontalInput)
{
horizontalInput = _horizontalInput;
}
public void OnJumpPressed ()
{
jump = true;
}
} and here is the code for the input manager:
[SerializeField] Movement movement;
[SerializeField] MouseLook mouseLook;
[SerializeField] GunMechanics gun;
PlayerControls controls;
PlayerControls.GroundMovementActions groundMovement;
Vector2 horizontalInput;
Vector2 mouseInput;
private void Awake()
{
controls = new PlayerControls();
groundMovement = controls.GroundMovement;
// groundMovement.[action].performed += context => do something
groundMovement.HorizontalMovement.performed += ctx => horizontalInput = ctx.ReadValue<Vector2>();
groundMovement.Jump.performed += _ => movement.OnJumpPressed();
groundMovement.MouseX.performed += ctx => mouseInput.x = ctx.ReadValue<float>();
groundMovement.MouseY.performed += ctx => mouseInput.y = ctx.ReadValue<float>();
groundMovement.Shoot.performed += _ => gun.Shoot();
}
private void Update()
{
movement.RecieveInput(horizontalInput);
mouseLook.RecieveInput(mouseInput);
}
private void OnEnable()
{
controls.Enable();
}
private void OnDestroy()
{
controls.Disable();
}
}
While this code works, I found a problem with it. When I spam the WASD a bunch of times, both mouseLook and movement get stuck for some reason. As in, I am stuck moving in one direction and can't rotate the camera until I input the same direction I am stuck in.
This is a huge problem when implementing this movement in a movement shooter. Is there any way to fix this?
Answer by MegaBomb_Unity · Oct 11, 2021 at 10:44 AM
Nevermind. Found the solution all by myself. Just turn the controller.move script into an if statement.
verticalVelocity.y += gravity * Time.deltaTime;
if (verticalVelocity != Vector3.zero) {
controller.Move(verticalVelocity * Time.deltaTime);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613043955im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity input system: TapInteraction does not produce a value when using ReadValue() 0 Answers
How do I have multiple controls for the same function? (Controller and Keyboard) 1 Answer
How can I smooth out and unlock the movement with the input system? 0 Answers
Why does the keyboard control what button is selected? 2 Answers