Java/Unity/Android - How do I Make a keycode event triggered when pressing a button?

Viewed 65

so, I have a WebView loading a html5 game on it, and it's working fine.

But the problem is that some games are only supporting pc, and I can't play them in my mobile. so For Example, on a PC, if a user is moving the player using arrow keys, It will not be able to move in my mobile since I don't have arrow keys.

What I am trying to do is make some buttons on my android project that trigger the same Keycode events on a PC.

To specify even more, instead of having 4 arrow keys, I want to have 4 buttons that triggers the same keycode that would make the player move.

Is there a way to make it happen?

1 Answers

The best way to solve this problem is to use Unity new input system. This method allows you to set the equivalent of your keys in both Android and PC as well as in switch or xbox controls or more..., after setup you can share all the code to one point to using the Vector input of directions.

enter image description here

After defining the actions in the new input system, you can synchronize the controls by reference to the Action. Below is an example of how to use the new input system.

private PlayerControls _controls;
public void Start()
{
    _controls = new PlayerControls();
    _controls.Enable();
}
public void Update()
{
    Vector3 input = _controls.GamePlay.Move.ReadValue<Vector2>();
    
    transform.position += input * Time.deltaTime;
}
Related