Code Monkey home page Code Monkey logo

xinput_gamepad_controller's Introduction

Xinput GamePad Controller (v2.5)

Slightly better version of the xinput implementation for unity based on the amazing work done by speps (https://github.com/speps/XInputDotNet), allows to control four gamepads with a very simple to use API and easily map computer inputs to gamepad button using a custom inspector.

##How to use : Simply drag the "LaunchGamePadController" script in on any object in your scene (preferably an empty). Then in your character controller script for instance, add the following :

GamePadController.Controller gamePad; //Declaring a variable for our gamepad
void Start()
{
    gamePad = GamePadController.GamePadOne; //GamePadOne for first controller GamePadTwo for the second one, and so on...
}

And then to use the buttons just write :

gamePad.A.Pressed (if you want to check that the button A has been pressed [one frame])
gamePad.A.Held (if you want to check that the button A is held)
gamePad.A.Released (if you want to check that the button A has been released [one frame])
gamePad.A.Zero (is the default state)

To get the axis inputs just use :

gamePad.LeftStick.X (for the x axis of the leftstick).

Thanks to speps' work you can also use vibrations :

gamePad.SetVibration(100,100) //for 100% intensity on both motors
gamePad.StopVibration() //to stop the vibration
gamePad.SetVibration(100,100,5) //to have the gamepad vibrate for 5 seconds then stop

You can also very easily map computer inputs to the gamepad inputs, that way the 'v' key of your keyboard will represent the A button of your gamePad :

if(gamePad.A.Pressed)
    Debug.Log("Pressed")

that way this code will allow you to have both inputs (computer and gamepad) working at the same time or only one at a time (if the gamePad is not connected) with a single line of code.

//--> Additional Objectives
//add dualstick for 2d games
//add deadzone function
//add more than 4 controllers to gamepadcontroller
//change gamepad number classes to enum, ex : GamePad.One

//-->Computer Inputs
//First controllers values get copied in all other controllers (so as not to rewrite them)
//allow change of key or button or axis at runtime (public class and variables plus custom functions)
//Check if button is axis and transform axis values to button
//make some deafult parameters (Ex : Right Stick is always axis, if button is axis, then only one direction)
//Add other default Parameters (Ex : LeftStick X&Y already have Horizontal and Vertical written, etc...)
//Add smart autocomplete
//Use custom editor for "enable controller"
//Use structs (same as GamePadController) for computer inputs
//Save/Load preconfigured inputs in a file

xinput_gamepad_controller's People

Contributors

magrossegit avatar onepage avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.