-
Notifications
You must be signed in to change notification settings - Fork 1
Input Manager
The input manager is a wrapper for the Oculus plugin to detect input on the Oculus Touch controllers.
To start using the Input Manager to detect input on the Touch Controllers, there is an InputManager
prefab located in Assets -> Prefabs -> Core
that you can drag into the scene. The Input Manager has methods that allow other scripts to know when input has been detected. The two main ways of being notified when input has been detected is to either attach a callback that will be called when the specific button has been pressed/touched, or to check a boolean. See below for more information.
Before you can start using the input manager, you must:
- Drag the
InputManager
prefab into your scene. - In the script that you want to access in the input manager script, save a reference to
InputManager.Instance
or useInputManager.Instance
directly. Example:
InputManager instance;
instance = InputManager.Instance
//Start using the InputManager's methods like so:
instance.AttachInputHandler(MyMethodName, InputState.ON_PRESS, Button.A);
//Can also attach like this:
InputManager.Instance.AttachInputHandler(MyMethodName, InputState.ON_PRESS, Button.A);
Example Scene: In Assets > Scenes
you can find the Input
scene, which demos how to use the Input Manager.
To attach callback functions that will be called whenever a specific input is detected, you will need to use the AttachInputHandler
function of InputManager.cs
. However, depending on the type of input you want to detect, you will want to call the function with a different signature. The AttachInputHandler
often uses defined enums in InputManager.cs
, and you can find more information about the InputManager enums here.
If all you need is to call a function whenever a button is pressed/touched/released, you can use the simplest function signature: AttachInputHandler(Action callback, InputState state, Button button)
. The callback
you need to pass in is a function that does not take in any parameters at all. state
is the input state you want to detect (ON_PRESS
, ON_TOUCH
, ON_RELEASE
, ON_TOUCH_RELEASE
), and the button
is the specific button you are checking for.
If I had a parameter-less function in my script called TurnCubeRed
that I wanted to be called whenever button A is pressed, I would do the following: InputManager.Instance.AttachInputHandler(TurnCubeRed, InputState.ON_PRESS, Button.A)
;
On the Oculus Touch Controllers, you can access the force currently applied on the trigger and grip buttons. If you would like to access this information, you must use the AttachInputHandler(Action<float> callback, Trigger trigger)
or AttachInputHandler(Action<float> callback, Grip grip)
function depending on whether you want to monitor the trigger or grip button. You would pass in a function that takes a float as a parameter, and pass in the type of Trigger or Grip you want to monitor. The float that will be passed to your function will range from 0.0 to 1.0
, with 0.0 being not pressed at all and 1.0 being fully pressed.
If I had a function TurnCubeRed(float force)
where I check if the right hand trigger force is greater than 0.5 before turning my cube red, I would attach it to the Input Manager like so: InputManager.Instance.AttachInputHandler(TurnCubeRed, Trigger.RIGHT);
.
If you want to see how far the joystick has been moved, you will need to use the AttachInputHandler(Action<Vector2> callback, Joystick joystick)
function signature. This function takes in a callback that has a Vector2 as a parameter. The Vector2 details, from -1.0f to 1.0f
how far the joystick has moved in the x and y directions, respectively.
If I had a function TurnCubeRed(Vector2 amountMoved)
that I want to use to monitor the left-hand joystick, I would attach it to the Input Manager like so: InputManager.Instance.AttachInputHandler(TurnCubeRed, Joystick.LEFT);
.
It is very important to detach your callbacks after attaching them to prevent memory leaks. In most cases, you would attach your callbacks in one of the lifecycle functions (like Start
or OnEnable
), and you should detach each callback that you detach in the corresponding lifecycle function (OnDestroy
or OnDisable
, for example). Just as there is the AttachInputHandler
function with different signatures to attach various callbacks, there is the DetachInputHandler
function with different signatures that you use to detach different kinds of callbacks. The usage of DetachInputHandler
is very similar to how you use AttachInputHandler
.
If you want to use a boolean check to see if a button is being pressed or touched instead of attaching a callback, you can use the IsButtonPressed
or IsButtonTouched
functions to check that and pass as a parameter in the button that you want to check. The only thing is that these functions must be called in your script's Update
function, or they must be called by a function that eventually gets called in the Update
function.
-
A
: A button on the right hand -
B
: B button on the right hand -
X
: X button on the left hand -
Y
: Y button on the left hand -
RIGHT_TRIGGER
: index finger trigger button on the right controller -
LEFT_TRIGGER
: index finger trigger button on the left controller -
RIGHT_GRIP
: grip button on the right controller -
LEFT_GRIP
: grip button on the left controller -
RIGHT_JOYSTICK
: joystick on the right controller -
LEFT_JOYSTICK
: joystick on the left controller
-
RIGHT
: Trigger on the right controller -
LEFT
: Trigger on the left controller
-
RIGHT
: Grip on the right controller -
LEFT
: Grip on the left controller
-
RIGHT
: Joystick on the right controller -
LEFT
: Joystick on the left controller