Unity new input system returning 0 or not working

Viewed 1203

I am trying to use Unity's new input system to simply read the mouse position. I created a StandardMap InputActionMap asset, with a CursorPosition InputAction configured to read the mouse position ("Value & Vector2" action).

I should be able to read the value of the mouse using something like:

Vector2 pointerPosition = 
 theInputMapAsset.FindActionMap("StandardMap").FindAction("CursorMovement").ReadValue<Vector2>();

Unfortunately, pointerPosition always returns 0,0! I tried everything and it doesn't seem to work...

EDIT: This situation happens, it turns out, if you have no PlayerInput component that defines a default input handler as noted in: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Components.html The answer below does still apply, as it correctly warns that an input map must be explicitly enabled if there is no player input component.

1 Answers

Turns out you need to Enable() the InputActionMap before using it! Calling the following code enabled the input correctly:

theInputMapAsset.FindActionMap("StandardMap").Enable()

(of course you should probably save references to the action map/actions instead of calling FindActionMap every frame)

Related