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. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Simple Input - use custom input providers like joysticks, UI buttons and d-pads [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Mar 5, 2018.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594


    SimpleInput is an improvement over Unity's legacy Input system that allows you to use custom input providers like on-screen joysticks, UI buttons and d-pads. In other words, it lets you simulate e.g. Input.GetAxis when a button is pressed or a virtual joystick is dragged. It also supports using custom axes and buttons that don't necessarily exist in Edit-Project Settings-Input.

    Using SimpleInput is as simple as replacing Input with SimpleInput in your scripts. In addition to a bunch of helper components, includes the following plug 'n' play prefabs: joystick, dpad, 4-directional arrows, 2-directional arrows and steering wheel.

    Please note that Unity's new Input System is not supported.

    Asset Store: https://assetstore.unity.com/packages/tools/input-management/simple-input-system-113033
    Also available at: https://github.com/yasirkula/UnitySimpleInput
    Discord: https://discord.gg/UJJt549AaV
    WebGL demo: http://yasirkula.net/SimpleInputDemo/
    Support the developer ☕

    Enjoy!
     
    Last edited: Oct 28, 2021
    dyupa and theANMATOR2b like this.
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Now available on Asset Store!
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Updated to version 1.1:

    - added pinch, swipe and rotate gesture inputs
    - added WebGL demo (of the example scene)
     
    adityamohanta27 and dyupa like this.
  4. molul

    molul

    Joined:
    Jan 21, 2017
    Posts:
    15
    I'm very interested in this, will try tomorrow. Though I'm not sure: is it a touch input manager, or any input like physical controllers?

    I've been waiting for a way to play my unity projects with my 8bitdo controllers, but axes and buttons are not correctly detected. Would this asset let me use them?
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    This is more like a touch input manager. It has built-in input elements made with UI system (like virtual joystick and dpad) and supports basic touch inputs like swipe and pinch. If 8bitdo controller doesn't work well with Unity's built-in Input system, then probably this asset won't be of much help. But if the issue with the axes/buttons is that they are just mapped to wrong axes/buttons, then you can use this asset to remap them to custom axes/buttons.
     
  6. molul

    molul

    Joined:
    Jan 21, 2017
    Posts:
    15
    That sounds good (that's the problem, yes). Thanks for the reply!

    Btw, what should I do to correctly detect the input axes from the 8bitdo controller? I mean, what's the workflow for remapping axes with this plugin?
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    I may have made a mistake by saying "remapping" because the value isn't really remapped (moved) to the new axis but rather copied. So, if you "remap" axis A to axis B, the value of A won't be transferred over to B but instead, both A and B will have the same value (copied value).

    To copy the value of axis A to axis B (or button A to button B), you can use the following code (where previousAxis represents axis A and newAxis represents axis B):

    Code (CSharp):
    1. public string previousAxis;
    2. public SimpleInput.AxisInput newAxis = new SimpleInput.AxisInput();
    3.  
    4. public string previousButton;
    5. public SimpleInput.ButtonInput newButton = new SimpleInput.ButtonInput();
    6.  
    7. void OnEnable()
    8. {
    9.     newAxis.StartTracking();
    10.     newButton.StartTracking();
    11.  
    12.     SimpleInput.OnUpdate += OnUpdate;
    13. }
    14.  
    15. void OnDisable()
    16. {
    17.     newAxis.StopTracking();
    18.     newButton.StopTracking();
    19.  
    20.     SimpleInput.OnUpdate -= OnUpdate;
    21. }
    22.  
    23. void OnUpdate()
    24. {
    25.     newAxis.value = Input.GetAxisRaw( previousAxis );
    26.     newButton.value = Input.GetButton( previousButton );
    27. }
    Now SimpleInput.GetAxis(previousAxis) and SimpleInput.GetAxis(newAxis) will both return the same value (the value of Input.GetAxis(previousAxis)).

    I'm guessing that you want to move the value of axis A to axis B, not copy. You can still kinda achieve this by creating a new axis in Edit-Project Settings-Input and configuring that axis to receive input from joysticks only. Afterwards, you can "remap" that axis to axis B. This way, keyboard/mouse input will stay intact.
     
  8. karlsevenvii

    karlsevenvii

    Joined:
    May 3, 2016
    Posts:
    4
    does this joystick works fine in unity 2018? i used and coded several joystick code in the latest unity 2018 but it only works fine in editor. once compile to android, it only work once then after unpressing the handle the handle wont move anymore
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    I haven't installed Unity 2018 yet, but I'll give it a try after installing it. I don't see a reason for the joystick to fail in a build, though. Let me know if you receive any error messages.
     
  10. karlsevenvii

    karlsevenvii

    Joined:
    May 3, 2016
    Posts:
    4
    yeah i mean thats even weirder there is no error ive been using my joystick script since 4 then just modify it to work on every new unity3d version that is being released but this time im having a hard time checking whats wrong as there are no error the almost all joystick ive tried in the asset store including the crossplaform that is included in standard assets has this behavior, it works fully in editor once compiled in android it will work once, then after untouch the screen and touch again the handle doesnt drag at all
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    I've tested the example scene (Unity 2018.1.3f1) on a Nexus 5 API 25 x86 emulator and the joystick seems to work fine. Feel free to try it yourself, as well ^^
     
  12. acharyajay007

    acharyajay007

    Joined:
    Apr 14, 2018
    Posts:
    1
    I Could not understand how to rebind Input. can you give me hint how to use it?
     
  13. karlsevenvii

    karlsevenvii

    Joined:
    May 3, 2016
    Posts:
    4
    hi when i tried your plugin, and use the joystick at the right the capsule just rotates and doesnt walk
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    @acharyajay007 This plugin doesn't really rebind the input because it is not possible to "move" the value of one axis to the other. For example, if you try to remap "Horizontal" axis to "Horizontal2" axis, both of these axes will have the same value because the value will be "copied over". Value of "Horizontal" is provided internally by Unity's input system and it doesn't seem possible to move (or rebind) it to another axis runtime.

    However, let's say you want to have an axis to move the player horizontally and you want its keys to be remappable runtime. In this case, I'd recommend you use an original axis name (e.g. "MoveHorizontal") with the following code:

    Code (CSharp):
    1. public SimpleInput.AxisInput axis = new SimpleInput.AxisInput( "MoveHorizontal" );
    2.  
    3. public KeyCode axisNegativeKey = KeyCode.A;
    4. public KeyCode axisPositiveKey = KeyCode.D;
    5.  
    6. void OnEnable()
    7. {
    8.     axis.StartTracking();
    9.     SimpleInput.OnUpdate += OnUpdate;
    10. }
    11.  
    12. void OnDisable()
    13. {
    14.     axis.StopTracking();
    15.     SimpleInput.OnUpdate -= OnUpdate;
    16. }
    17.  
    18. void OnUpdate()
    19. {
    20.     if( Input.GetKey( axisNegativeKey ) )
    21.         axis.value = -1f;
    22.     else if( Input.GetKey( axisPositiveKey ) )
    23.         axis.value = 1f;
    24.     else
    25.         axis.value = 0f;
    26. }
    By default, this script sets the value of MoveHorizontal axis via A and D keys but these keys can be remapped runtime. Now, you can use SimpleInput.GetAxis( "MoveHorizontal" ) instead of Input.GetAxis( "Horizontal" ) in your script(s) to move the player horizontally.

    To can also set the value of axis.Key to change "MoveHorizontal" to e.g. "MoveHorizontal2" runtime.

    @karlsevenvii Is it working fine on WebGL demo? It should work when moving the joystick up and down. If it is only problematic while testing in editor, can you check the axes of the joystick in the Inspector?
     
  15. saneangel8

    saneangel8

    Joined:
    May 1, 2017
    Posts:
    4
    Hi,
    My long time dream is to have the ability to setup a joystick when the player starts.
    ala Retro Pie:


    Would this package allow me to head down that path?
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    I'm afraid not. SimpleInput doesn't have any special logic for external hardware. In the description, by joystick I meant on-screen joystick.
     
  17. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,248
    Hello,

    can it be used the Control the Cursor in a UI Scene like the mouse?
    So I can Control the Cursor Position with the mouse, but also with the Joystick, if a Joystick is attached?
    Would that work for a X-Box or PS4, too?

    Thanks :)
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    That'd be a nice feature but unfortunately not.
     
  19. ElegantUniverse

    ElegantUniverse

    Joined:
    Sep 13, 2018
    Posts:
    76
    hello thank u for your amazing plugin
    there are 2 problems for me , why the animations in 2D (for example walking,Running) don't work?
    when i use keyboard ,player walks as its animation set , but when i use joystick walk animation just slides not walk
    and the second problem is when i click on screen for first time to pull joystick to right or left fire1 performs

    I used this tutorial
    https://www.youtube.com/watch?v=3BLGBrTpqh4
    Code (CSharp):
    1.  
    2.   void walking() {
    3.  
    4.       if (!isDead)
    5.       {
    6.           dirx = SimpleInput.GetAxisRaw("Horizontal") * walkspeed;
    7.       }
    8.       if (dirx == 0)
    9.       {
    10.           anim.SetBool("isWalking", false);
    11.           anim.SetBool("isRunning", false);
    12.       }
    13. if (Mathf.Abs(dirx) == 5 && rg.velocity.y == 0)
    14.       {
    15.           anim.SetBool("isWalking", true);
    16.       }
    17.    }
    18.  
    19.  
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Joystick returns a value in range [-1,1]. It returns 1 in its Horizontal axis only if you snap its thumb perfectly to the right edge. I'd suggest you to try using
    Mathf.Abs(dirx) > 0
    instead of
    Mathf.Abs(dirx) == 5
    .
     
    ElegantUniverse likes this.
  21. ElegantUniverse

    ElegantUniverse

    Joined:
    Sep 13, 2018
    Posts:
    76
    thank you it works
     
  22. javirv2001

    javirv2001

    Joined:
    Dec 1, 2018
    Posts:
    1
    Hi! I didn't understand completely how the "button input swipe gesture" works... Can you give me an example of how it works?
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    It is useful when e.g. you want to trigger SimpleInput.GetButtonDown("Jump") when user swipes their finger upwards on the screen. Simply add this component to a RectTransform inside which you want to detect the finger swipe, set its "Button" to "Jump" and set the "Swipe Amount" as something like (0,10).
     
  24. martongamestudio

    martongamestudio

    Joined:
    Jan 26, 2019
    Posts:
    1
    Hi.
    ... and what about under 2018.3 and try moving Ellen... (3d Game Kit Lite asset)
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Inside PlayerInput.Update, change Input with SimpleInput. Then, add the following 2 lines to the Awake function:

    Code (CSharp):
    1. Input.simulateMouseWithTouches = false; // Prevents Fire1 from getting called when touching the screen on mobile devices
    2. Cinemachine.CinemachineCore.GetInputAxis = SimpleInput.GetAxis; // Providing input to Cinemachine via SimpleInput
    Set CameraRig.KeyboardAndMouseFreeLookRig's "Y Axis" to CamY and "X Axis" to CamX because the default CameraX and CameraY axes are affected by finger swipes on mobile platforms regardless of whether or not the finger is dragging a joystick or etc.

    Now, create a UI-Canvas object in your scene and drag&drop the Joystick prefab into it. Also create two UI-Image objects in it and add "Button Input UI" component to them. Set their Button values as "Jump" and "Fire1" respectively. Finally, add Touchpad component to the canvas and set its axes as CamX and CamY. You may want to invert the value of the input on both axes, as well.

    Tested on S8 without any issues.

    Screenshot.png
     
  26. andreagames

    andreagames

    Joined:
    Feb 2, 2019
    Posts:
    2
    Hi, I am trying to set the left and right d-pad and I am having a problem,
    when my character moves forward the UI stays in place do you know how I can fix or remedy the problem?
     
  27. andreagames

    andreagames

    Joined:
    Feb 2, 2019
    Posts:
    2
    Nevermind I had my layers wrong I apologise
     
    yasirkula likes this.
  28. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    hello,
    is it possible to grab D-Pad x/y axis of PS4 controller as keycode??
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    You can first create new axes for the x and y values of the dpad in Input Settings (see "D-Pad X Axis" and "D-Pad Y Axis" here for reference) and then use the following script to map these axes to keycodes:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DpadToKeycode : MonoBehaviour
    4. {
    5.     private const float THRESHOLD = 0.75f;
    6.    
    7.     public string dpadXAxis = "D-Pad X Axis", dpadYAxis = "D-Pad Y Axis";
    8.  
    9.     public SimpleInput.KeyInput xPositive = new SimpleInput.KeyInput();
    10.     public SimpleInput.KeyInput xNegative = new SimpleInput.KeyInput();
    11.     public SimpleInput.KeyInput yPositive = new SimpleInput.KeyInput();
    12.     public SimpleInput.KeyInput yNegative = new SimpleInput.KeyInput();
    13.    
    14.     private void OnEnable()
    15.     {
    16.         xPositive.StartTracking();
    17.         xNegative.StartTracking();
    18.         yPositive.StartTracking();
    19.         yNegative.StartTracking();
    20.        
    21.         SimpleInput.OnUpdate += OnUpdate;
    22.     }
    23.  
    24.     private void OnDisable()
    25.     {
    26.         xPositive.StopTracking();
    27.         xNegative.StopTracking();
    28.         yPositive.StopTracking();
    29.         yNegative.StopTracking();
    30.  
    31.         SimpleInput.OnUpdate -= OnUpdate;
    32.     }
    33.  
    34.     private void OnUpdate()
    35.     {
    36.         float x = Input.GetAxisRaw( dpadXAxis );
    37.         float y = Input.GetAxisRaw( dpadYAxis );
    38.        
    39.         if( x >= THRESHOLD )
    40.         {
    41.             xPositive.value = true;
    42.             xNegative.value = false;
    43.         }
    44.         else if( x <= -THRESHOLD )
    45.         {
    46.             xPositive.value = false;
    47.             xNegative.value = true;
    48.         }
    49.         else
    50.         {
    51.             xPositive.value = false;
    52.             xNegative.value = false;
    53.         }
    54.        
    55.         if( y >= THRESHOLD )
    56.         {
    57.             yPositive.value = true;
    58.             yNegative.value = false;
    59.         }
    60.         else if( y <= -THRESHOLD )
    61.         {
    62.             yPositive.value = false;
    63.             yNegative.value = true;
    64.         }
    65.         else
    66.         {
    67.             yPositive.value = false;
    68.             yNegative.value = false;
    69.         }
    70.     }
    71. }
    P.S. I don't know if the reference Input Settings are valid for all joysticks, i.e. whether or not dpad is always mapped to 7th and 8th joystick axes. If these values can change from joystick to joystick, then you'll need to use a dedicated plugin that can read dpad values from any joystick properly.
     
  30. HonKasumi

    HonKasumi

    Joined:
    Apr 25, 2018
    Posts:
    44
    Hi, i personally didnt use this asset becuase of multiplayer issue, im using photon and dont know how to impliment this into a photon game, i have a other control input, it works fine but when i go online in game the joystick and touchfield dont work, i still dont know the issue, maeby this is more easy?
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    I'm guessing that this is an issue on the networking-side, not on the input-side. All players will receive input from Horizontal and Vertical axes and forward these input to the server in some way, either it be via a NetworkedTransform-like component or directly sending a Vector2. Are you sure that you have some sort of Transform synchronizer set up in your project?
     
  32. barmando

    barmando

    Joined:
    Feb 6, 2020
    Posts:
    3
    i'm stuck here:

    i'm testing with the SAMPLESCENE >
    Karting Microgame - Unity Learn - Unity Tutorials

    - I've imported the asset
    -i've add a steeringwheel to the canvas
    - i think i have to update the script below: Keyboardinput, but there i'm stuck because it's full with Input.GetKey 's, and in your information stands that that couldn't be replaced.
    - how can i add/or replace the keyborad with the buttons?

    Thank you in advance.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    namespace KartGame.KartSystems
    {
    /// <summary>
    /// A basic keyboard implementation of the IInput interface for all the input information a kart needs.
    /// </summary>
    public class KeyboardInput : MonoBehaviour, IInput
    {
    public float Acceleration
    {
    get { return m_Acceleration; }
    }
    public float Steering
    {
    get { return m_Steering; }
    }
    public bool BoostPressed
    {
    get { return m_BoostPressed; }
    }
    public bool FirePressed
    {
    get { return m_FirePressed; }
    }
    public bool HopPressed
    {
    get { return m_HopPressed; }
    }
    public bool HopHeld
    {
    get { return m_HopHeld; }
    }

    float m_Acceleration;
    float m_Steering;
    bool m_HopPressed;
    bool m_HopHeld;
    bool m_BoostPressed;
    bool m_FirePressed;

    bool m_FixedUpdateHappened;

    void Update ()
    {
    if (Input.GetKey (KeyCode.UpArrow))
    m_Acceleration = 1f;
    else if (Input.GetKey (KeyCode.DownArrow))
    m_Acceleration = -1f;
    else
    m_Acceleration = 0f;

    if (Input.GetKey (KeyCode.LeftArrow) && !Input.GetKey (KeyCode.RightArrow))
    m_Steering = -1f;
    else if (!Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.RightArrow))
    m_Steering = 1f;
    else
    m_Steering = 0f;

    m_HopHeld = Input.GetKey (KeyCode.Space);

    if (m_FixedUpdateHappened)
    {
    m_FixedUpdateHappened = false;

    m_HopPressed = false;
    m_BoostPressed = false;
    m_FirePressed = false;
    }

    m_HopPressed |= Input.GetKeyDown (KeyCode.Space);
    m_BoostPressed |= Input.GetKeyDown (KeyCode.RightShift);
    m_FirePressed |= Input.GetKeyDown (KeyCode.RightControl);
    }

    void FixedUpdate ()
    {
    m_FixedUpdateHappened = true;
    }
    }
    }
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    You can use steering wheel input by changing the following lines with
    m_Steering = SimpleInput.GetAxis("Horizontal");
    :

    Code (CSharp):
    1. if (Input.GetKey (KeyCode.LeftArrow) && !Input.GetKey (KeyCode.RightArrow))
    2. m_Steering = -1f;
    3. else if (!Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.RightArrow))
    4. m_Steering = 1f;
    5. else
    6. m_Steering = 0f;
    For other ones, if you'd like to receive input from UI elements, simply replace Input.GetKey functions with SimpleInput.GetKey, create a UI Image object for each distinct key and attach "Key Input UI" component to each of these Image objects. Then, set each component's Key value accordingly.
     
  34. barmando

    barmando

    Joined:
    Feb 6, 2020
    Posts:
    3

    THANK YOU!!
    how does it work for accelerating and brake then?
     
  35. barmando

    barmando

    Joined:
    Feb 6, 2020
    Posts:
    3
    I've Got it, Easy!

    Thanks!!
     
    yasirkula likes this.
  36. purnachandra2020

    purnachandra2020

    Joined:
    Mar 27, 2020
    Posts:
    3
    Hi how to add simple input to 2D game kit,im struck here please help

    using UnityEngine;

    namespace Gamekit2D
    {
    public class PlayerInput :

    InputComponent, IDataPersister
    {
    public static PlayerInput

    Instance
    {
    get { return s_Instance; }
    }

    protected static PlayerInput

    s_Instance;


    public bool HaveControl { get {

    return m_HaveControl; } }

    public InputButton Pause = new

    InputButton(KeyCode.Escape,

    XboxControllerButtons.Menu);
    public InputButton Interact = new

    InputButton(KeyCode.E,

    XboxControllerButtons.Y);
    public InputButton MeleeAttack =

    new InputButton(KeyCode.K,

    XboxControllerButtons.X);
    public InputButton RangedAttack =

    new InputButton(KeyCode.O,

    XboxControllerButtons.B);
    public InputButton Jump = new

    InputButton(KeyCode.Space,

    XboxControllerButtons.A);
    public InputAxis Horizontal = new

    InputAxis(KeyCode.D, KeyCode.A,

    XboxControllerAxes.LeftstickHorizontal);
    public InputAxis Vertical = new

    InputAxis(KeyCode.W, KeyCode.S,

    XboxControllerAxes.LeftstickVertical);
    [HideInInspector]
    public DataSettings dataSettings;

    protected bool m_HaveControl =

    true;

    protected bool m_DebugMenuIsOpen

    = false;

    void Awake ()
    {
    if (s_Instance == null)
    s_Instance = this;
    else
    throw new UnityException

    ("There cannot be more than one

    PlayerInput script. The instances are "

    + s_Instance.name + " and " + name +

    ".");
    }

    void OnEnable()
    {
    if (s_Instance == null)
    s_Instance = this;
    else if(s_Instance != this)
    throw new UnityException

    ("There cannot be more than one

    PlayerInput script. The instances are "

    + s_Instance.name + " and " + name +

    ".");



    PersistentDataManager.RegisterPersister

    (this);
    }

    void OnDisable()
    {


    PersistentDataManager.UnregisterPersister

    (this);

    s_Instance = null;
    }

    protected override void

    GetInputs(bool fixedUpdateHappened)
    {
    Pause.Get

    (fixedUpdateHappened, inputType);
    Interact.Get

    (fixedUpdateHappened, inputType);
    MeleeAttack.Get

    (fixedUpdateHappened, inputType);
    RangedAttack.Get

    (fixedUpdateHappened, inputType);
    Jump.Get(fixedUpdateHappened,

    inputType);
    Horizontal.Get(inputType);
    Vertical.Get(inputType);

    if (Input.GetKeyDown

    (KeyCode.F12))
    {
    m_DebugMenuIsOpen = !

    m_DebugMenuIsOpen;
    }
    }

    public override void GainControl

    ()
    {
    m_HaveControl = true;

    GainControl(Pause);
    GainControl(Interact);
    GainControl(MeleeAttack);
    GainControl(RangedAttack);
    GainControl(Jump);
    GainControl(Horizontal);
    GainControl(Vertical);
    }

    public override void

    ReleaseControl(bool resetValues = true)
    {
    m_HaveControl = false;

    ReleaseControl(Pause,

    resetValues);
    ReleaseControl(Interact,

    resetValues);
    ReleaseControl(MeleeAttack,

    resetValues);
    ReleaseControl(RangedAttack,

    resetValues);
    ReleaseControl(Jump,

    resetValues);
    ReleaseControl(Horizontal,

    resetValues);
    ReleaseControl(Vertical,

    resetValues);
    }

    public void

    DisableMeleeAttacking()
    {
    MeleeAttack.Disable();
    }

    public void EnableMeleeAttacking

    ()
    {
    MeleeAttack.Enable();
    }

    public void

    DisableRangedAttacking()
    {
    RangedAttack.Disable();
    }

    public void

    EnableRangedAttacking()
    {
    RangedAttack.Enable();
    }

    public DataSettings

    GetDataSettings()
    {
    return dataSettings;
    }

    public void SetDataSettings

    (string dataTag,

    DataSettings.PersistenceType

    persistenceType)
    {
    dataSettings.dataTag =

    dataTag;
    dataSettings.persistenceType

    = persistenceType;
    }

    public Data SaveData()
    {
    return new Data<bool, bool>

    (MeleeAttack.Enabled,

    RangedAttack.Enabled);
    }

    public void LoadData(Data data)
    {
    Data<bool, bool>

    playerInputData = (Data<bool, bool>)data;

    if (playerInputData.value0)
    MeleeAttack.Enable();
    else
    MeleeAttack.Disable();

    if (playerInputData.value1)
    RangedAttack.Enable();
    else
    RangedAttack.Disable();
    }

    void OnGUI()
    {
    if (m_DebugMenuIsOpen)
    {
    const float height = 100;

    GUILayout.BeginArea(new

    Rect(30, Screen.height - height, 200,

    height));

    GUILayout.BeginVertical

    ("box");
    GUILayout.Label("Press

    F12 to close");

    bool meleeAttackEnabled =

    GUILayout.Toggle(MeleeAttack.Enabled,

    "Enable Melee Attack");
    bool rangeAttackEnabled =

    GUILayout.Toggle(RangedAttack.Enabled,

    "Enable Range Attack");

    if (meleeAttackEnabled !=

    MeleeAttack.Enabled)
    {
    if

    (meleeAttackEnabled)


    MeleeAttack.Enable();
    else


    MeleeAttack.Disable();
    }

    if (rangeAttackEnabled !=

    RangedAttack.Enabled)
    {
    if

    (rangeAttackEnabled)


    RangedAttack.Enable();
    else


    RangedAttack.Disable();
    }
    GUILayout.EndVertical();
    GUILayout.EndArea();
    }
    }
    }
    }
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    You should find Input.GetButton lines inside InputButton class and replace them with SimpleInput.GetButton. Same goes for InputAxis class, as well. Then you can use the SimpleInput prefabs to provide input.
     
  38. purnachandra2020

    purnachandra2020

    Joined:
    Mar 27, 2020
    Posts:
    3
    Still confusing,im a beginner to unity,can you please help more?
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    I'd recommend you to read the documentation (it's short) and then take a look at the scripts I've used in the example scene. There is nothing complicated going on so I think you'll easily grasp it.
     
  40. purnachandra2020

    purnachandra2020

    Joined:
    Mar 27, 2020
    Posts:
    3
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    namespace Gamekit2D
    {
    public abstract class InputComponent : MonoBehaviour
    {
    public enum InputType
    {
    MouseAndKeyboard,
    Controller,
    }


    public enum XboxControllerButtons
    {
    None,
    A,
    B,
    X,
    Y,
    Leftstick,
    Rightstick,
    View,
    Menu,
    LeftBumper,
    RightBumper,
    }


    public enum XboxControllerAxes
    {
    None,
    LeftstickHorizontal,
    LeftstickVertical,
    DpadHorizontal,
    DpadVertical,
    RightstickHorizontal,
    RightstickVertical,
    LeftTrigger,
    RightTrigger,
    }


    [Serializable]
    public class InputButton

    {
    public KeyCode key;
    public XboxControllerButtons controllerButton;

    public bool Down { get; protected set; }
    public bool Held { get; protected set; }
    public bool Up { get; protected set; }
    public bool Enabled
    {
    get { return m_Enabled; }
    }

    [SerializeField]
    protected bool m_Enabled = true;
    protected bool m_GettingInput = true;

    //This is used to change the state of a button (Down, Up) only if at least a FixedUpdate happened between the previous Frame
    //and this one. Since movement are made in FixedUpdate, without that an input could be missed it get press/release between fixedupdate
    bool m_AfterFixedUpdateDown;
    bool m_AfterFixedUpdateHeld;
    bool m_AfterFixedUpdateUp;

    protected static readonly Dictionary<int, string> k_ButtonsToName = new Dictionary<int, string>
    {
    {(int)XboxControllerButtons.A, "A"},
    {(int)XboxControllerButtons.B, "B"},
    {(int)XboxControllerButtons.X, "X"},
    {(int)XboxControllerButtons.Y, "Y"},
    {(int)XboxControllerButtons.Leftstick, "Leftstick"},
    {(int)XboxControllerButtons.Rightstick, "Rightstick"},
    {(int)XboxControllerButtons.View, "View"},
    {(int)XboxControllerButtons.Menu, "Menu"},
    {(int)XboxControllerButtons.LeftBumper, "Left Bumper"},
    {(int)XboxControllerButtons.RightBumper, "Right Bumper"},
    };

    public InputButton(KeyCode key, XboxControllerButtons controllerButton)
    {
    this.key = key;
    this.controllerButton = controllerButton;
    }

    public void Get(bool fixedUpdateHappened, InputType inputType)
    {
    if (!m_Enabled)
    {
    Down = false;
    Held = false;
    Up = false;
    return;
    }

    if (!m_GettingInput)
    return;

    if (inputType == InputType.Controller)
    {
    if (fixedUpdateHappened)
    {
    Down = Input.GetButtonDown(k_ButtonsToName[(int)controllerButton]);
    Held = Input.GetButton(k_ButtonsToName[(int)controllerButton]);
    Up = Input.GetButtonUp(k_ButtonsToName[(int)controllerButton]);

    m_AfterFixedUpdateDown = Down;
    m_AfterFixedUpdateHeld = Held;
    m_AfterFixedUpdateUp = Up;
    }
    else
    {
    Down = Input.GetButtonDown(k_ButtonsToName[(int)controllerButton]) || m_AfterFixedUpdateDown;
    Held = Input.GetButton(k_ButtonsToName[(int)controllerButton]) || m_AfterFixedUpdateHeld;
    Up = Input.GetButtonUp(k_ButtonsToName[(int)controllerButton]) || m_AfterFixedUpdateUp;

    m_AfterFixedUpdateDown |= Down;
    m_AfterFixedUpdateHeld |= Held;
    m_AfterFixedUpdateUp |= Up;
    }
    }
    else if (inputType == InputType.MouseAndKeyboard)
    {
    if (fixedUpdateHappened)
    {
    Down = Input.GetKeyDown(key);
    Held = Input.GetKey(key);
    Up = Input.GetKeyUp(key);

    m_AfterFixedUpdateDown = Down;
    m_AfterFixedUpdateHeld = Held;
    m_AfterFixedUpdateUp = Up;
    }
    else
    {
    Down = Input.GetKeyDown(key) || m_AfterFixedUpdateDown;
    Held = Input.GetKey(key) || m_AfterFixedUpdateHeld;
    Up = Input.GetKeyUp(key) || m_AfterFixedUpdateUp;

    m_AfterFixedUpdateDown |= Down;
    m_AfterFixedUpdateHeld |= Held;
    m_AfterFixedUpdateUp |= Up;
    }
    }
    }

    public void Enable()
    {
    m_Enabled = true;
    }

    public void Disable()
    {
    m_Enabled = false;
    }

    public void GainControl()
    {
    m_GettingInput = true;
    }

    public IEnumerator ReleaseControl(bool resetValues)
    {
    m_GettingInput = false;

    if (!resetValues)
    yield break;

    if (Down)
    Up = true;
    Down = false;
    Held = false;

    m_AfterFixedUpdateDown = false;
    m_AfterFixedUpdateHeld = false;
    m_AfterFixedUpdateUp = false;

    yield return null;

    Up = false;
    }
    }

    [Serializable]
    public class InputAxis
    {
    public KeyCode positive;
    public KeyCode negative;
    public XboxControllerAxes controllerAxis;
    public float Value { get; protected set; }
    public bool ReceivingInput { get; protected set; }
    public bool Enabled
    {
    get { return m_Enabled; }
    }

    protected bool m_Enabled = true;
    protected bool m_GettingInput = true;

    protected readonly static Dictionary<int, string> k_AxisToName = new Dictionary<int, string> {
    {(int)XboxControllerAxes.LeftstickHorizontal, "Leftstick Horizontal"},
    {(int)XboxControllerAxes.LeftstickVertical, "Leftstick Vertical"},
    {(int)XboxControllerAxes.DpadHorizontal, "Dpad Horizontal"},
    {(int)XboxControllerAxes.DpadVertical, "Dpad Vertical"},
    {(int)XboxControllerAxes.RightstickHorizontal, "Rightstick Horizontal"},
    {(int)XboxControllerAxes.RightstickVertical, "Rightstick Vertical"},
    {(int)XboxControllerAxes.LeftTrigger, "Left Trigger"},
    {(int)XboxControllerAxes.RightTrigger, "Right Trigger"},
    };

    public InputAxis(KeyCode positive, KeyCode negative, XboxControllerAxes controllerAxis)
    {
    this.positive = positive;
    this.negative = negative;
    this.controllerAxis = controllerAxis;
    }

    public void Get(InputType inputType)
    {
    if (!m_Enabled)
    {
    Value = 0f;
    return;
    }

    if (!m_GettingInput)
    return;

    bool positiveHeld = false;
    bool negativeHeld = false;

    if (inputType == InputType.Controller)
    {
    float value = Input.GetAxisRaw(k_AxisToName[(int)controllerAxis]);
    positiveHeld = value > Single.Epsilon;
    negativeHeld = value < -Single.Epsilon;
    }
    else if (inputType == InputType.MouseAndKeyboard)
    {
    positiveHeld = Input.GetKey(positive);
    negativeHeld = Input.GetKey(negative);
    }

    if (positiveHeld == negativeHeld)
    Value = 0f;
    else if (positiveHeld)
    Value = 1f;
    else
    Value = -1f;

    ReceivingInput = positiveHeld || negativeHeld;
    }

    public void Enable()
    {
    m_Enabled = true;
    }

    public void Disable()
    {
    m_Enabled = false;
    }

    public void GainControl()
    {
    m_GettingInput = true;
    }

    public void ReleaseControl(bool resetValues)
    {
    m_GettingInput = false;
    if (resetValues)
    {
    Value = 0f;
    ReceivingInput = false;
    }
    }
    }

    public InputType inputType = InputType.MouseAndKeyboard;

    bool m_FixedUpdateHappened;

    void Update()
    {
    GetInputs(m_FixedUpdateHappened || Mathf.Approximately(Time.timeScale,0));

    m_FixedUpdateHappened = false;
    }

    void FixedUpdate()
    {
    m_FixedUpdateHappened = true;
    }

    protected abstract void GetInputs(bool fixedUpdateHappened);

    public abstract void GainControl();

    public abstract void ReleaseControl(bool resetValues = true);

    protected void GainControl(InputButton inputButton)
    {
    inputButton.GainControl();
    }

    protected void GainControl(InputAxis inputAxis)
    {
    inputAxis.GainControl();
    }

    protected void ReleaseControl(InputButton inputButton, bool resetValues)
    {
    StartCoroutine(inputButton.ReleaseControl(resetValues));
    }

    protected void ReleaseControl(InputAxis inputAxis, bool resetValues)
    {
    inputAxis.ReleaseControl(resetValues);
    }
    }
    }
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Simply replace Input.GetAxisRaw with SimpleInput.GetAxisRaw, Input.GetButtonDown with SimpleInput.GetButtonDown and etc.
     
  42. unity_WYEforWhxxbO0A

    unity_WYEforWhxxbO0A

    Joined:
    Mar 23, 2020
    Posts:
    1
    Hello sir I have been using your plugin for sometime and it worked absolutely well but it stopped working once I updated my unity to 2019 version from 2018 If I type simple input in my script it says
    Severity Code Description Project File Line Suppression State
    Error CS0103 The name 'SimpleInput' does not exist in the current context Assembly-CSharp C:\Users\nafis\OneDrive\Documents\TrafficRush\Assets\moveob.cs 58 Active
    please help
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Sometimes, Visual Studio throws an error but you can still enter Play mode in Unity. If that's the case for you, you can ignore this error, it will probably go away after restarting Unity. Otherwise, have you moved the SimpleInput package to some other location or maybe created an Assembly Definition File anywhere in your project?
     
  44. jimmyedagawa78

    jimmyedagawa78

    Joined:
    Apr 23, 2020
    Posts:
    2
    hi, I'm currently trying to use the unity creator kit to create a game because I'm new at coding, and I'm not sure what I have to do here. I added simple input but it doesn't seem to work. I tried the KeyInputUI and it says value false next to the key selection list. Not sure what to do

    Code (CSharp):
    1. using RPGM.Core;
    2. using RPGM.Gameplay;
    3. using UnityEngine;
    4. namespace RPGM.UI
    5. {
    6.     /// <summary>
    7.     /// Sends user input to the correct control systems.
    8.     /// </summary>
    9.     public class InputController : MonoBehaviour
    10.     {
    11.         public float stepSize = 0.1f;
    12.         GameModel model = Schedule.GetModel<GameModel>();
    13.         public enum State
    14.         {
    15.             CharacterControl,
    16.             DialogControl,
    17.             Pause
    18.         }
    19.         State state;
    20.         public void ChangeState(State state) => this.state = state;
    21.         void Update()
    22.         {
    23.             switch (state)
    24.             {
    25.                 case State.CharacterControl:
    26.                     CharacterControl();
    27.                     break;
    28.                 case State.DialogControl:
    29.                     DialogControl();
    30.                     break;
    31.             }
    32.         }
    33.         void DialogControl()
    34.         {
    35.             model.player.nextMoveCommand = Vector3.zero;
    36.             if (SimpleInput.GetKeyDown(KeyCode.LeftArrow))
    37.                 model.dialog.FocusButton(-1);
    38.             else if (SimpleInput.GetKeyDown(KeyCode.RightArrow))
    39.                 model.dialog.FocusButton(+1);
    40.             if (SimpleInput.GetKeyDown(KeyCode.Space))
    41.                 model.dialog.SelectActiveButton();
    42.         }
    43.         void CharacterControl()
    44.         {
    45.             if (SimpleInput.GetKey(KeyCode.UpArrow))
    46.                 model.player.nextMoveCommand = Vector3.up * stepSize;
    47.             else if (SimpleInput.GetKey(KeyCode.DownArrow))
    48.                 model.player.nextMoveCommand = Vector3.down * stepSize;
    49.             else if (SimpleInput.GetKey(KeyCode.LeftArrow))
    50.                 model.player.nextMoveCommand = Vector3.left * stepSize;
    51.             else if (SimpleInput.GetKey(KeyCode.RightArrow))
    52.                 model.player.nextMoveCommand = Vector3.right * stepSize;
    53.             else
    54.                 model.player.nextMoveCommand = Vector3.zero;
    55.         }
    56.     }
    57. }
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Assuming that you've configured the KeyInputUI object's Key property from the Inspector, your code looks correct. If your KeyInputUI object doesn't have any other components, try adding an Image component to it (you can set its color alpha value to 0 to make it invisible). Also make sure that there is an EventSystem object in the Hierarchy.
     
    Hodin likes this.
  46. Hodin

    Hodin

    Joined:
    Feb 11, 2016
    Posts:
    6
    Thanks for this great asset!

    I want to know if I set the dynamic options to true, Is possible keep it always visible, if nothing is pressing I move the joystick back to the original position.
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Hodin likes this.
  48. Hodin

    Hodin

    Joined:
    Feb 11, 2016
    Posts:
    6
    Thanks a lot. I created two more options to my needs. Works like a charm!

     
    yasirkula likes this.
  49. Clintelex

    Clintelex

    Joined:
    Sep 22, 2018
    Posts:
    9
    Hello
    Good work with the plug-in, but I have a small probs.
    I have set it up to work with my game using the AxisInputUI, all works well except at intervals some buttons trigger on their own simultaneously when I try it on my Android device. I can't figure this out.
    Please help.
     
  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,594
    Perhaps somehow Unity's Input.GetAxis is triggered by your touchs and it in turn triggers SimpleInput.GetAxis. You can use a custom axis like "Horizontal2" instead of the built-in ones ("Horizontal"). Alternatively, you can set SimpleInput.TrackUnityInput to false on Android devices so that Unity's Input.GetAxis doesn't trigger SimpleInput.GetAxis on those devices.