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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Call an Input

Discussion in 'Scripting' started by UnaiLz, Jun 26, 2018.

  1. UnaiLz

    UnaiLz

    Joined:
    Dec 12, 2015
    Posts:
    46
    Yeah, I know, this is basic stuff, but what I'm trying is not the classic

    Code (CSharp):
    1. if (Input.GetButtonDown("Jump"))
    2.     {
    3.         Do.stuff();
    4.     }

    I don't know how hard or easy this coud be, but I want to call an Input by switching a bool, something like

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (isJumpPress == true)
    4.         {
    5.             Input.GetButtonDown("Jump");
    6.         }
    7.     }

    What I want is to make the "Jump" input callable via script, without touching the space button or any button.

    Any idea?? Thank you :)
     
    Last edited: Jul 2, 2018
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (isJumpPress)
    4.         {
    5.             if (Input.GetButtonDown("Jump")) {
    6.               // Do something?
    7.             }
    8.         }
    9.     }
    Like so?

    You can collapse it to:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (isJumpPress && Input.GetButtonDown("Jump")) {
    4.            // Do something
    5.         }
    6.     }
     
  3. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    I think the
    Code (CSharp):
    1. Input.GetButtonDown("")
    returns a bool and the way its used is as shown above.

    To call it, well you write as shown above, in an if statement, or like this

    Code (CSharp):
    1.  
    2. bool x;
    3. x = Input.GetButtonDown("");
    4.  
    Which would set the variable x to true if the button is being pressed and false if it is not.
     
  4. UnaiLz

    UnaiLz

    Joined:
    Dec 12, 2015
    Posts:
    46
    I've tried something like that, but the thing is that I want to make the button being pressed when the bool is true, and not make the bool true when the button is pressed.

    I am trying to make virtual buttons with vuforia and it coud be very useful if I can call the input's using them, but actually I don't know if this can be done xD
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    You'd make a wrapper class reading Input. So something like MyInput, which does:

    Code (csharp):
    1. public static bool JumpPressed { get; private set; }
    2.  
    3. void Update() {
    4.     JumpPressed = Input.GetButtonDown("Jump");
    5. }
    6.  
    7. //in other scripts, check:
    8. if(MyInput.JumpPressed)
    And then whenever you want to override the inputs with something else, you just update your MyInput to do that.
     
  6. UnaiLz

    UnaiLz

    Joined:
    Dec 12, 2015
    Posts:
    46
    Done and working, but with this method I still have to push the physical button to make it work, what I need is to be able to push it only via script.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    If you only check input in Update, you could do something like:

    Code (csharp):
    1. public static bool JumpPressed;
    2.  
    3. void Update() {
    4.     if(Input.GetButtonDown("Jump"))
    5.         JumpPressed = true;
    6. }
    7.  
    8. void LateUpdate() {
    9.     JumpPressed = false;
    10. }
    Then you can set JumpPressed to true in one script, and check it in another.

    The issue there is execution order, if the script that checks this runs before the script that check it, it's a bit useless. You can handle that through script execution order, through the new (but somewhat complex and experimental) PlayerLoop system, or through some other means.
     
    UnaiLz likes this.
  8. UnaiLz

    UnaiLz

    Joined:
    Dec 12, 2015
    Posts:
    46
    So I shoud use MyInput to get all the inputs and make a bool and then work with those bool's as input?

    In this way, I coud just make a bool true with code and emulate the reaction.

    I'm going to try, thank you!!

    Edit: it worked, I'll still have to change a lot of stuff but definitely this helped a lot, thank you!
     
    Last edited: Jul 3, 2018