Search Unity

Changing Input.GetKey to Touch Screen

Discussion in 'Scripting' started by troycoulter90, Mar 6, 2019.

  1. troycoulter90

    troycoulter90

    Joined:
    Mar 6, 2019
    Posts:
    7
    Hi,

    I am extremely new to Unity, creating apps and coding. None the less i have followed a tutorial by Brackeys ( https://www.youtube.com/playlist?list=PLPV2KyIb3jR53Jce9hP7G5xC4O9AgnOuL ) to create a Cube based 3D game i have absolutely no problems until i exported onto my Android device and realized i have no way to control the Cube. I have tried a few things like changing it to Input.Touch and such but to no prevail. Any assistance showing how to change the script below to a script that has ability to use touch screen. such as where ever the finger is move the player object to the finger location.. or even touch left side to go left touch right to go right.

    using UnityEngine;

    public class PlayerMovement : MonoBehaviour{

    // This is used to reference the Rigidbody componant called "rb"
    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;

    // Update is called once per frame
    // Always use "FixedUpdate" when "messing" with physics
    void FixedUpdate()
    {
    // Add a forward force
    rb.AddForce(0, 0, forwardForce * Time.deltaTime);

    if (Input.GetKey("d"))
    {
    rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
    if (Input.GetKey("a"))
    {
    rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);

    }

    if (rb.position.y < -1f)
    {
    FindObjectOfType<GameManager>().EndGame();
    }
    }
    }



    Any help would be extremely welcome and appreciated. Thanks in advance!
     
    Last edited: Mar 6, 2019
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    You can create UI buttons in the editor, and setup your canvas to scale to appropriate screen orientation, then have those call methods in your script that can do the same things as hitting a key from your script.

    For example:
    Code (CSharp):
    1.     public void LeftHit()
    2.     {
    3.          rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    4.     }
    And on your UI Button there will be an event, where you will add the gameobject that has the script attached (perhaps some input handling empty gameobject that holds just the input script), and then call the method that takes the input like above, which has to be a public method.
     
    troycoulter90 likes this.
  3. troycoulter90

    troycoulter90

    Joined:
    Mar 6, 2019
    Posts:
    7
    MD_Reptile i appreciate that, if I am understanding correctly i create a new Ui button with the script you posted and attach to player for touch movement? Sorry i am very new to the Unity program and CSharp coding. Basically im trying to convert that script i posted into a touch enabled script. Cheers.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    You will have a UI Button which is a gameobject on a canvas in the scene. Set it up to be arrow directions, and have each direction be assigned its own method call on a script which is attached to a gameobject, in the scene, but not the player, because the player gameobject might get destroyed and reinstantiated and stuff like that during gameplay, so its best to have some kind of manager object that never gets destroyed and persists between scenes, that handles all the input.

    To actually assign the button to call the method do this:
    (BeckStarDE's answer)
    https://answers.unity.com/questions/942622/how-do-you-call-a-function-with-a-button-unity-5-u.html
     
  5. troycoulter90

    troycoulter90

    Joined:
    Mar 6, 2019
    Posts:
    7
    I think i made buttons for my Welcome( press to start the game) and an Exit (press and it quits app on device). I am also pretty sure i have a game manager that never gets destroyed.( my game manager script)... could be wrong being a newb and all.

    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class GameManager : MonoBehaviour{

    bool gameHasEnded = false;

    public float restartDelay = 1f;

    public GameObject completeLevelUI;

    public void CompleteLevel ()
    {
    completeLevelUI.SetActive(true);
    }

    public void EndGame ()
    {
    if (gameHasEnded == false)
    {
    gameHasEnded = true;
    Debug.Log("Game Over!");
    Invoke("Restart", restartDelay);
    }

    }

    void Restart ()
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    }


    In your most recent comment you state arrow keys? Is that needed for touch input in terms of moving a cube left and right on an Android phone or tablet device?