- Home /
The question is answered, right answer was accepted
Input System Vector2 value doesn't go back to (0,0)
I'm struggling to get the hang of Unity's new input system. I've set up a Vector2 value called Aim which listens for input from the left stick on the gamepad. My player does move when I move the left stick, but it often won't stop moving when I release it. More specifically, it often fails to register a (0,0) value when the stick 'flicks' back to center too quickly, if that makes sense. My code looks like this:
private InputMaster controls;
[SerializeField] private float speed = 5.0f;
private Vector3 aimDirection;
private void Awake()
{
controls = new InputMaster();
controls.Airship.Aim.performed += ctx => aimDirection = ctx.ReadValue<Vector2>();
}
private void Update()
{
Debug.Log(aimDirection);
Vector3 movement = Vector3.forward * speed * Time.deltaTime + aimDirection;
transform.Translate(movement);
}
I wondered at first if it was a joystick drift issue, but when this issue happens, my ship will be moving at full speed and Debug.Log(aimDirection) will often return a value where x or y is fully stuck on 1.0 or -1.0. I've also tried setting the deadzone ridiculously high, but that doesn't improve anything.
I feel like I'm probably misunderstanding how this works but I'm having a hard time getting a grasp on this new system. Can anyone help?
Answer by narasu · Aug 18, 2020 at 10:24 AM
I found the solution. I added the following line to the Awake method:
controls.Airship.Aim.canceled += ctx => aimDirection = Vector2.zero;
Everything works as expected now.
Why is this even necessary? It feels like a hack. EDIT: Ahhhh nevermind, I see. You can also just do ctx.ReadValue<Vector2>()
again on the canceled
event. It's necessary because letting go of the stick doesn't count as performing the action, so it switches to the canceled
event.
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How is it possible to Lerp from a non initialized value to an initialized value? 1 Answer
Why does holding down any button cause ~20-30 FPS loss? 1 Answer
Touchscreen input: Moving the player using screen.width & screen.height 1 Answer