- Home /
InputSystem v1.0.0 stops working sometimes
Hi there! I've been playing around with the new InputSystem for a few days and everything seemed fine on my 2d Platformer but... I started noticing a weird behavior from time to time. I have a InputManager script that deals with settings properties to check if there was any inputs on Unity Events regarding these inputs, like below:
public class InputManager : MonoBehaviour
{
public static InputManager Instance = null;
public float VerticalInput { get { return MovementInput.y; } }
public float HorizontalInput { get { return MovementInput.x; } }
public Vector2 MovementInput { get; set; }
private void Awake()
{
if (Instance == null)
Instance = this;
else
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
public void OnMove(InputAction.CallbackContext context)
{
MovementInput = context.ReadValue<Vector2>();
}
}
Then, I have a simple HorizontalController script to deal with player movement on the horizontal axis given the input above, however, if I play for a few minutes I notice that the player starts to move by itself for a while in a certain direction and only stops if I press an arrow key again. My HorizontalController is below, and my Actions for the InputSystem are the same as the default (Move Action is of type "Value" and of control type "Vector2"):
public class HorizontalController: MonoBehaviour
{
private Rigidbody2D rb2d;
[SerializeField] private float horizontalSpeed;
private void Awake()
{
rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
rb2d.velocity = new Vector2(InputManager.Instance.HorizontalInput * horizontalSpeed, rb2d.velocity.y);
}
}
If anyone could help I'd really appreciate!
Does this behaviour only occur after a scene transition? If so, you need to change your singleton pattern to only destroy if the Instance isn't itself.
$$anonymous$$y game doesn't have game transitions yet since it's just a test for physics and input at the moment. It has just one scene and this basic scripts for movement and input.