How do I create and use custom input action?

I am new to Unity XR and still beginner in Unity as a whole.
Every tutorial I found either uses device based input or uses just the default actions.
When I try to use one of the default actions like I saw in a tutorial, everything works as expected.
But when I add new action (called “Primary” in this case), it shows errors when I try to use the new action in a script.
So the question is how do I use actions created by me?

You will need to add a serialized field to your script of type InputActionReference, and then drag your Primary action from the asset to the field in the Inspector window. Then you can add your performed callback to that action. You can either subclass ActionBasedController to add the extra field or just add it to the script with your callback directly.

InputActionReference lets you store a reference to an Input Action contained within an Input Action Asset. You could also use an InputActionProperty (which is used in ActionBasedController) which allows you to either reference an Input Action contained in an asset, or directly define the Input Action on the component instead of in the asset.

1 Like