- Home /
better way to handle gamepad support
if (Input.GetKey(KeyCode.W))
{
if (canjump)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jump);
jumpsleft--;
}
}
if (Input.GetKey(KeyCode.JoystickButton0))
{
if (canjump)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jump);
jumpsleft--;
}
}
this is the current way I handle it pls help me find a better way
Answer by tategarringer · Dec 13, 2020 at 05:02 AM
Unity has a relatively new input system available in the Package Manager. Brackey's also has a tutorial video from 2019 featuring a very basic implementation of this input system which harnesses event subscription to fire events on button press.
With this input system, you'd subscribe to an event by passing a method to the event delegate in Awake()
(and unsubscribing in Destroy()
so you don't incur memory leak) and then when the button is pressed, an event is raised and the method passed will fire. I would highly recommend watching the linked video, it does a pretty nice job of laying out the format and the basics.
Your answer
Follow this Question
Related Questions
Setting up game pad 2 Answers
Generic USB Controller axis issue 0 Answers
Weird Input Manager behaviour 1 Answer
How can I disable gamepad input, for my UI? 1 Answer
joystick dpad axes snapping to 1 0 Answers