- Home /
How to add controller input to keyboard input
Hi guys, I already have keyboard input set up, but I want to add an option to use a controller in my game. How would I add this on top of my character controller and cinemachine camera?
Answer by ddooms · Jan 06, 2021 at 03:29 AM
Best practice is to assume you want both, even if you don't, before writing any code. Using axis instead of buttons where you can is a great way to increase the possibility that ant particular controller will be supported. Best solution that I can offer you is to get familiar with the new input system (please learn it in a new project as it will overwrite all current work in terms of Input). The new input system has multiple device support and allows you to map ranges of values to your controls rather than a simple binary "on or off" like with a keyboard. All you do is have your keyboard profile setup to return a value rather than a button press, and read that value as a float or normalized vector. An example of this is using WASD to move. Set your speed to be dependant on the value between -1 and 1. Then set the keyboard to use s as inverted value, and the w key as value. If you then setup the controller to use the left stick axis to map the same value between -1 and 1, you get the full range of motion without compromising your keyboard setup.