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

Mobile Input Systen

Discussion in 'Input System' started by Zemox, Jun 9, 2020.

  1. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    So I have a little problem, it´s the first time i am working on a mobile game and I cant figure out how to get my controls:
    if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxHeight)
    {
    Instantiate(effect, transform.position, Quaternion.identity);
    targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
    transform.position = targetPos;
    }
    else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight)
    {
    Instantiate(effect, transform.position, Quaternion.identity);
    targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
    transform.position = targetPos;
    }
    mapped to a UI button (I have a little arrow that points up and on that points down) is there any way to maybe directly assign (KeyCode.UpArrow) (KeyCode.DownArrow) to the UI button?
     
  2. EdoC-QWERTY

    EdoC-QWERTY

    Joined:
    Feb 1, 2020
    Posts:
    75
    If the button is on the UI you have to use the inspector to assign a function to the button.
    The code you are using is for keyboard inputs.


    Select the button in the scene. In the inspector you have a On click field. I attached an image to the post.

    There you can drag the object the script is attached to (you can try to drag the player game object, so you can access th script and move it) and then you can choose a function to call when the button is clicked.
    For a movement you can also add a new On click event and instead On click (that triggers only when the button is pressed) make it register when the button is pressed or released and call the desired function.

    For example let's take the arrow up button. Change the function in the script to something like this


    Code (CSharp):
    1.  
    2. public void ArrowUpPressed()
    3. {
    4. if (transform.position.y < maxHeight)
    5. {
    6. Instantiate(effect, transform.position, Quaternion.identity);
    7. targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
    8. transform.position = targetPos;
    9. }
    10. }
    11.  
    In the inspector you have a

    On button down
    player object ArrowUpPressed()
    player script

    So you will have 2 UI buttons.
    ArrowUp and Arrow Down.
    On each object you have two events in the inspector. One for when the button is pressed and another for when it is released.

    I hope this helps
     

    Attached Files:

    Last edited: Jun 9, 2020
  3. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    Thanks for the answer <3