Search Unity

Do you now how to simulate input keys ?

Discussion in 'Scripting' started by Dj_Zuczyslaw, Jun 26, 2014.

  1. Dj_Zuczyslaw

    Dj_Zuczyslaw

    Joined:
    Mar 20, 2014
    Posts:
    31
    Hello, do you know how to simulate input keys ?
    My character move and jump. When i press "spacebar" my character jump because this is described in the input manager in unity.
    I want this: when I push GUI buton, that command will simulate "spacebar" push.
    I'm very bad from coding and i don't know how to do this. I'm using C#.

    I pasted this: Input.GetKeyDown(KeyCode.Space);
    But i think this command only chceck that "spacebar" is pressed...
    Please Help!.
     
  2. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    You cannot, I suggest you use an event system for your inputs, such as Advanced C# Messenger(free). Then you would broadcast an event when a key is pressed, and the character movement script would have a listener. This way you can easily add a broadcaster whenever you want.
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    You could use something like the following:
    Code (csharp):
    1.  
    2. public class YourBehaviour : MonoBehaviour {
    3.     private bool _fireKey;
    4.  
    5.     private void Update() {
    6.         if (Input.GetKeyDown(KeyCode.Space) || _fireKey) {
    7.             // Do fire logic here...
    8.  
    9.             _fireKey = false;
    10.         }
    11.     }
    12.  
    13.     private void OnGUI() {
    14.         if (GUI.Button(new Rect(0, 0, 100, 100), "Fire!")) {
    15.             // Trigger fire behaviour on next update!
    16.             _fireKey = true;
    17.         }
    18.     }
    19. }
    20.  
    or alternatively:

    Code (csharp):
    1.  
    2. public class YourBehaviour : MonoBehaviour {
    3.     private void Update() {
    4.         if (Input.GetKeyDown(KeyCode.Space))
    5.             Fire();
    6.     }
    7.  
    8.     private void OnGUI() {
    9.         if (GUI.Button(new Rect(0, 0, 100, 100), "Fire!"))
    10.             Fire();
    11.     }
    12.  
    13.     private void Fire() {
    14.         // Do fire logic here...
    15.     }
    16. }
    17.  
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  5. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    This breaks the idea of single purpose scripts though, you should not really be putting GUI stuff in a character movement script.
     
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    We stick a layer between the Input class and our game. Everything in our game uses a "GameInput" class and not Input directly.

    This way we can change what causes the "fireCommand" in GameInput, and not have to touch any other class.
     
    sootie8 likes this.
  7. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    This is good, do you use static method calls or events(or something else)?
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  9. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    I did that before, but the class can become insanely large when you messing with axis input(or xbox controllers in my case). I did used a new static method for each key / axis though, so you would type InputManager.IsKeyX() then it would check for X.

    Edit: or a singleton as you said :p
     
  10. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Yeah, we already have all the static bools in our GameInput class. That's three bools that need to get set for every action. (Eg: fire, fireDown, fireUp) I was thinking about switching it to enum bit flags and making Get(bitflag), GetDown(bitflag), GetUp(bitflag) functions, very much like how Unity's Input class works with their Input.GetKey(KeyCode) functions and enums.

    This would reduce the code down a ton while making it much easier to add new types of input.

    If we need to add a ton more types of game input, then I'll do that. For the purposes of our current game though, the GameInput class is pretty much done and I don't expect to update it again until we start another project. Wish I was paid to just clean up code all day long! =p
     
  11. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    Refactoring :p
     
    GarthSmith likes this.
  12. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    I've always liked to use an interface (or abstract class) to define how an entity like a Player gets its input. Then write specific implementations which know how to respond to a certain type of input (or many types if you prefer).

    This abstraction helps when handling multiple players and can also be used to control entities from an AI script or network connection. It also means you can keep your individual implementations lightweight.
     
  13. Dj_Zuczyslaw

    Dj_Zuczyslaw

    Joined:
    Mar 20, 2014
    Posts:
    31
    Ok, thank u for answers, I will fight with this :).