Search Unity

(Released)InputManager: Change Input mapping using code (multiplayer update incoming)

Discussion in 'Assets and Asset Store' started by Muuskii, Sep 7, 2012.

  1. Muuskii

    Muuskii

    Joined:
    Jul 7, 2012
    Posts:
    5
    Good news everyone! ;)

    InputManager; A way to remap keyboard input into actions through scripting was just accepted into the asset store. Why is this script necessary? Because as you begin coding in Unity you may come across a moment when you write something like this:

    Code (csharp):
    1.  
    2. if(Input.GetKey( KeyCode.W))
    3. {
    4.     //Move the player forward
    5. }
    6.  
    But what happens when a player doesn't want to use the w key to move forward? You would need a system that keeps track of what key is attached to what action and gives you information about if that key or it's alternative key is being pressed. Unfortunately Unity doesn't already do this for you.

    With InputManager the above code would change to:

    Code (csharp):
    1.  
    2. if(InputManager.GetKey( "Forward" ))
    3. {
    4.     //Move the player forward
    5. }
    6.  
    And now the player will move forward when either the w key is pressed or the up arrow key, and the player can change which keys are used without the above code knowing or caring. It all works the same!

    Next comes support for GetAxis which works in much the same way as the default GetAxis function, except that the default one is based on KeyCodes whereas InputManager's GetAxis function is based off actions like Forward vs Back. For example:

    Code (csharp):
    1.  
    2. Vector3 moveDir = Vector3.zero;
    3.  
    4. //These are bound to the WASD keys and arrow keys regardless of who you are
    5. moveDir.z = Input.GetAxis("Vertical");
    6. moveDir.x = Input.GetAxis("Horizontal");
    7.  
    8. //These are defined by whatever keys the player chose the last time they looked at the menu in game
    9. moveDir.z = InputManager.GetAxis("Vertical");
    10. moveDir.x = InputManager.GetAxis("Horizontal");
    11.  
    12. //either way, the player moves.
    13. transform.Translate(moveDir * moveSpeed * * Time.deltaTime);
    14.  
    Key binds are saved to file
    Assuming your players wants to spend more of their time playing your game and less time reconfiguring the hotkeys how they like; You probably want to save their choices to a permanent place! Conveniently InputManager saves them to an .xml file where the player can easily access them and make changes even when the game isn't running. However, if you don't want to use .xml or don't have access to it; There are methods to save to PlayerPrefs instead.

    Key Combinations
    InputManager is a continually growing script as I add features to it everyday. Key Combinations are activated when the player holds down a series of keys at the same time. Examples of times when you might need this is if you're looking for when the player hits Control+Z (Or command+Z ) in order to activate the Undo function.

    Another example is if you're making an RTS (real time strategy game) you could setup a key combination where if the player hits Control+1 the currently selected units will be assigned to control group one. And then from that point on whenever the player hits the 1 key it will automatically select that group to issue commands.

    Making Multiplayer Games
    As it happens, I was working on a script named "Network Input Serialize" when I received word that InputManager was added to the asset store. The Serializer will make it much easier for new Unity game developers to make authoritative multiplayer games without having to deal with some of the lower level management that is involved with getting networking to . . . work!

    The reason why InputManager, with the serializer script included will make muliplayer game development easier is because it overcomes the issue mentioned before: The server neither knows nor cares what button the player is actually pressing so long as it knows that a certain action is desired by the player.

    It should also should be a fix to the issue that when someone imports a prefab like the Third Person Controller (which is based off local input) it doesn't work as one would immediately expect when used in a muliplayer environment.

    NetworkInputSerialize.cs should be done sometime soon and will be added into the newest release of InputManager along with some other changes that I've been working on. For more information on InputManager and for a full Tutorial on using InputManager please check out muuski's awesome Unity website!!! (Okay maybe some of that is an overstatement :p )

    And if you have any suggestions or complaints; Contact me

    Thanks!
     
  2. Muuskii

    Muuskii

    Joined:
    Jul 7, 2012
    Posts:
    5
    Reserved for more edits! :D
     
  3. AustinK

    AustinK

    Joined:
    Dec 10, 2012
    Posts:
    86
    I know this is fairly old, but I thought I'd ask: Does this also allow you to map some default keys depending on the system they are on? For instance, if I'm on Xbox, maybe I want jump to be joystick button 0, but on ps3 I want jump to be joystick button 1, but in code I still want to just call InputManager.GetAxis("Jump");

    Thanks,
    Austin