Custom Input Controllers (PC and Mobile)

Modified on Sun, 15 Sep 2019 at 08:42 PM

Voxel Play provides default input controllers for PC and Mobile which can be repalced by yours.


In Voxel Play Environment inspector, expand the "Default Assets" section and look for the "Input Prefab" fields:



When Voxel Play starts, if the running platform is mobile, it will instantiate the "Input Prefab (Mobile)" or Input Prefab (PC) otherwise.

The prefabs can contain anything:

  • The PC prefab it just contains a script for reading the keyboard and mouse and updates the input state. The script is located in Voxel Play/Resources/VoxelPlay/InputControllers/PC/KeyboardMouseController.cs.
  • The Mobile prefab contains a Canvas element with some touch buttons and a script that reads user touches and updates the input state. The script can be found in Voxel Play/Resources/VoxelPlay/InputControllers/Mobile/DualTouchController.cs


Providing your own controllers


To provide your own controller, duplicate or create a new prefab which contains a script that reads the controls (keys, touches, etc) and updates the input state. The script must derive from VoxelPlayIniputController class:


public class MyController: VoxelPlayInputController { ... }


And must implement the following methods:


bool Initialize ();

Called to initialize your input controller (if needed)


void UpdateInputState ();

Called every frame so you can read player touches or keypresses and update the input state.


The easiest way to create your own controller is to duplicate the PC or Mobile default script and modify them. Add your script to the prefab and assign the prefab to Voxel Play Environment inspector.



The Input state


When VP runs, it automatically picks the appropriate controller for the current platform. The controllers update the input state every frame. What's the input state? It's a class that contains the status for certain buttons and provides some useful methods to read the state of keys etc.


To access the current input controller use the input public property from VoxelPlayEnvironment object.


Both classes (DualTouchController and KeyboardMouseController) derive from VoxelPlayInputController which defines the following properties and methods:


input.horizontalAxis

input.verticalAxis

input.mouseX

input.mouseY

input.mouseScrollWheel

input.screenPos

input.focused

input.enabled

input.initialized


The following methods return the state of "known button":

input.GetButton(...)

input.GetButttonDown(...)

input.GetButttonUp(...)


The list of "known buttons" are defined by the INPUT_BUTTON_NAMES enum:


    public enum INPUT_BUTTON_NAMES {
Button1,
Button2,
Jump,
Up,
Down,
LeftControl,
LeftShift,
LeftAlt,
Build,
Fly,
Crouch,
Inventory,
Light,
ThrowItem,
Action,
MiddleButton
    }


The input.GetButton(...) methods return the state for the given button:


    public enum INPUT_BUTTON_STATE {
Idle,
Down,
Up,
Pressed
    }




Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article