Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mouse and Gamepad data without InputManager

Discussion in 'Scripting' started by VortexStudios, Jun 13, 2014.

  1. VortexStudios

    VortexStudios

    Joined:
    Jul 16, 2012
    Posts:
    84
    Can I get the mouse and gamepad data without the default Unity's InputManager or I need to develop my own input implementation to do that?


    Thanks :)
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Yes, several methods don't use the InputManager including Input.mousePosition and Input.GetKey().
     
  3. VortexStudios

    VortexStudios

    Joined:
    Jul 16, 2012
    Posts:
    84
    Nice! And what about the gamepad axes and buttons??
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    You can get the buttons with Input.GetKeyDown(KeyCode.JoystickButton0) but not axes. That is a known limitation of the Input system is that you must go through the Input Manager for joystick axes.
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Just curious... is there a reason not to use the Input Manager? You can set up the axes in the Input Manager, and then never show the Input Manager to the player.
     
  6. VortexStudios

    VortexStudios

    Joined:
    Jul 16, 2012
    Posts:
    84
    Hum!! That solves our problem a lot :D
    So adapt the question to find a solution for the axis, can I re-create (add, rename the mappers is anything) the InputManager array by code??

    Garth, i'm doind some researches for educational and artist projects, and we need to create or destroy the mappers, we are implemening some codes to accept more than one mouse and keyboards too... So, if we can do that with the Unity we implement or own code, but is a little anoing to implement for Windows, Windows8, MACOS and Linux, so if we can do that with the unity we can use just virtual mapper to make everything much more easy ^^
     
  7. VortexStudios

    VortexStudios

    Joined:
    Jul 16, 2012
    Posts:
    84
    more people are looking for this, this feature is on suggestions, the current inputmanager is easy but isn't flexible :(
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    We use a middleman class that takes input from Input and converts it to whatever we need in our game. Nothing in our game reads Input directly, but instead reads our CustomInput class.

    CustomInput has a bool like "jumpButton" which behind the scenes can be true from button input, a joystick axis, or a gui button press. We can set the InputManager however we want and it only affects this one CustomInput class.
     
  9. VortexStudios

    VortexStudios

    Joined:
    Jul 16, 2012
    Posts:
    84
    But you need to create the axes in InputManager array??
    If this class isn't proprietary can you send the name and we will test here if it solves our problem ^^
     
  10. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I can't give out our code since it's not my code, and it is really written for a specific game.

    Here's an example I wrote real quick showing how one button might work. I've been thinking about using bit flags instead of a bunch of bools, but I don't really have a reason to go back and do that yet. This hasn't been tested.

    Code (csharp):
    1. public static class GameInput {
    2.  
    3.   public static bool jumpButton {
    4.     get {
    5.       UpdateInput();
    6.       return m_jumpButton;
    7.     }
    8.   }
    9.   static bool m_jumpButton;
    10.  
    11.   static void UpdateInput() {
    12.     if (lastFrameUpdated < Time.frameCount) {
    13.       // m_jumpButtonDown = m_jumpButtonUp = false;
    14.       if (Input.GetAxis("SomeAxis") > 0.5f || Input.GetMouseButton(0) || Input.GetKey(KeyCode.Space)) {
    15.         if (!m_jumpButton) {
    16.           m_jumpButton = true;
    17.           // m_jumpButtonDown = true;
    18.       }
    19.       else {
    20.         if (m_jumpButton) {
    21.           m_jumpButton = false;
    22.           // m_jumpButtonUp = true;
    23.         }
    24.       }
    25.       lastFrameUpdated = Time.frameCount;
    26.     }
    27.   }
    28.  
    29.   static int lastFrameUpdated { get; set; }
    30.  
    31. }
     
    VortexStudios likes this.