Introduction to Hinput, the simple gamepad manager for Unity
May 17, 2020 17:32 · 461 words · 3 minute read
Hi there! My name’s Henri, and this is an introduction to Hinput, the simple gamepad manager for Unity. Hinput gives you access to up to eight controllers, including sticks, buttons, and vibrations (on Windows) with a super simple API. You do not need to setup any key bindings, and it automatically adapts to Windows, Mac, and Linux at runtime. Hinput features an example scene, thorough documentation, heavily commented code, and direct support from the developer. Me! Let’s move on to an example. In this scene, I have set up a cube. I would like that tube to turn red when I press A on my controller.
00:43 - To do that, I’m simply going to add a script to that cube, and then open the script in my text editor. In the Update method, I’m going to write if(Hinput.gamepad[0].A) This is going to return true if A is pressed, and false if it’s released. But it’s not quite what I want. I would like to know if A has been pressed right this frame. To do that, I’m simply going to add .
justPressed This is going to be true for exactly one frame when I press A. All I need to do now is to turn the cube red. So that’s: GetComponent
01:58 - So that’s a new Vector2 called stickPosition and it’s going to be equal to Hinput.gamepad[0]… But this time, I’m going to get the left stick of my gamepad. That’s going to return the position of the stick as a Vector2. All I need to do now is to add that to the position of my cube. So that’s: transform.position += new Vector3(stickPosition.x, stickPosition.y, 0); To make that code less framerate-dependent, I’m going to add a public float speed, and I’m going to multiply the movement by speed and by Time.deltaTime.
02:45 - Alright, Im getting back to my scene I will set the speed of my cube to 10, and if I’m pressing play and moving around the left stick of my controller a little bit… The cube moves! Alright that’s it for this short introduction to Hinput, the simple gamepad manager. The plugin’s available for free on the Unity Asset store, so feel free to have a look and give it a try for yourself, and don’t forget that if you have any question I’m available by email at hello@hinput.co Have fun! .