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

Multiple input bug

Discussion in 'Scripting' started by Skywolf829, Jan 6, 2016.

  1. Skywolf829

    Skywolf829

    Joined:
    Nov 21, 2015
    Posts:
    4
    Hey guys, thanks for any help in advance.

    I'm making a 2D sidescroller with movement controls handled with the arrow keys(up to jump, down to crouch, left/right to move). Currently these all work well, and adding spells to A, S, D, and F also worked. But I stumbled across something very odd.

    I was attempting to add a new spell bound to left shift + F. It worked by itself, but moving with it cause strange bugs.

    I could no longer move left while spamming the spell(holding down F + LSHIFT), which I can with all of my other spells(they all behave the exact same, just spawn a different prefab). Other than this, the spell works and no errors are caught.

    Then, while debugging and ripping my hair out, I found another bug with any spell bound to A. Like the rest of the abilities, I could run left or right and jump while shooting, but the spell bound to A could not jump while moving left for some reason. I could either face left and jump while shooting, or run left and shoot, but not both.

    I've changed key binds, and the number 1 at the top of the alphanumeric keyboard fixes my issue with the spell bound to A as well as shift + f, so I thought it was something with the input options Unity has set. I cleared all axes, but the problem still persists.

    Here's a snippet of the shooting code.
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.F))
    2. {
    3.        //Do the shift + f spell
    4. }
    5. else if (Input.GetKey(KeyCode.A))
    6. {
    7.       //Do the A spell
    8. }
    9. else if (Input.GetKey(KeyCode.S))
    10. {
    11.        //Do the S spell
    12. }
    13. else if (Input.GetKey(KeyCode.D))
    14. {
    15.        //Do the D spell
    16. }
    17. else if (Input.GetKey(KeyCode.F))
    18. {
    19.        //Do the F spell
    20. }
    And here's the movement code for getting input. Later the "horizontal" and "vertical" floats are used later to determine movement and crouching/jumping.
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.RightArrow))
    2. {
    3.       horizontal = 1;
    4. }
    5. else if (Input.GetKey(KeyCode.LeftArrow))
    6. {
    7.       horizontal = -1;
    8. }
    9. else if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.RightArrow) || !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow))
    10. {
    11.       horizontal = 0;
    12. }
    13.  
    14. if (Input.GetKey(KeyCode.DownArrow))
    15. {
    16.       vertical = -1;
    17. }
    18. else if (Input.GetKey(KeyCode.UpArrow))
    19. {
    20.       vertical = 1;
    21. }
    22. else if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.UpArrow) || !Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
    23. {
    24.       vertical = 0;
    25. }
    I've tried an boolean input array that stores the key-state for each key, but I was running into more bugs with that. Really at a loss for a fix that doesn't require a different set of keys. Ideally, ASDF will be the keys for the spells, but if no solution is found, input will have to be changed.

    I've done other research, and it seems Unity has had a problem with multiple input issues since 2010, but I haven't seen any updates to the issue.

    Sorry I don't have any real testable or reproducible code. Has anyone else run into this issue?
     
  2. hyiero

    hyiero

    Joined:
    Jun 12, 2015
    Posts:
    7
    Have you tried attempting doing it by GetKeyDown and then keeping that state stored somewhere of whatever key you just pressed and then can release its state on the KeyUp of that same key
     
    watson1423 likes this.
  3. Skywolf829

    Skywolf829

    Joined:
    Nov 21, 2015
    Posts:
    4
    Yes, I used an array of booleans, and each button I used in my game had a registered spot. For some reason, response was very spotty. I think I found the issue though for that key-state array solution, I was using FixedUpdate instead of Update. I'll try that tomorrow and let you know if I have any success.
    Currently I've moved my abilities to alphanumeric 1 2 3 and 4 and shift has been moved to space. Everything works well with this, but ideally it'd be changed to a s d and f with shift.
     
  4. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Try setting the scale of the object to the -value of what it was before, so then you are always facing right