Search Unity

Replace Input.GetKey with Input.GetTouch to ove in horizontal axis.

Discussion in 'Scripting' started by AmbarishGK, Jan 6, 2018.

  1. AmbarishGK

    AmbarishGK

    Joined:
    Dec 30, 2017
    Posts:
    3
    I am new to unity and been trying to implement touch input to the code. I need to replace the Input.GetKey with touch input. The player should be able to move left or right by touch the left or right side of the screen. Rigibody is a cube.

    public class move : MonoBehaviour {​
    public Rigidbody rb;
    public float ff = 1000f;
    public float dk = 200f;

    void FixedUpdate () {
    rb.AddForce(0, 0, ff*Time.deltaTime);
    if (Input.GetKey("d")) { rb.AddForce(dk * Time.deltaTime, 0, 0,ForceMode.VelocityChange); }
    if (Input.GetKey("a")) { rb.AddForce(-dk * Time.deltaTime, 0, 0,ForceMode.VelocityChange); }
    if (rb.position.y < -1) { FindObjectOfType<manager>().Endgame(); }
    }
    }
    Need to make it mobile friendly.
    Thanks in advance!​
     
  2. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    you can make OnGUI Button and then add the function to that button...like
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. //Be Sure to add using UnityEngine.UI
    7.  
    8. public class move : MonoBehaviour {
    9. public Rigidbody rb;
    10. public float ff = 1000f;
    11. public float dk = 200f;
    12.  
    13. void FixedUpdate () {
    14.     rb.AddForce(0, 0, ff*Time.deltaTime);
    15.    if (rb.position.y < -1) { FindObjectOfType<manager>().Endgame(); }
    16. }
    17.  
    18.  
    19.  
    20.     void OnGUI() {
    21.    
    22.                  //adjust the position by changing these Screen.width-100, 0, 100, 40 values
    23.                 if( GUI.Button(new Rect (Screen.width-100, 0, 100, 40), "Left") ) {
    24.                 rb.AddForce(dk * Time.deltaTime, 0, 0,ForceMode.VelocityChange);
    25.             }
    26.             //adjust the position by changing these 10,10,100,200 values
    27.             if( GUI.Button(new Rect (10, 10, 100, 20), "Right") ) {
    28.             rb.AddForce(-dk * Time.deltaTime, 0, 0,ForceMode.VelocityChange);
    29.     }
    30.    
    31.    
    32. }

    Or You can do something like this if you wanna adjust or customize your button visually then here's the script:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class move : MonoBehaviour {
    5. public Rigidbody rb;
    6. public float ff = 1000f;
    7. public float dk = 200f;
    8.  
    9. void FixedUpdate () {
    10.     rb.AddForce(0, 0, ff*Time.deltaTime);
    11.    if (rb.position.y < -1) { FindObjectOfType<manager>().Endgame(); }
    12. }
    13.  
    14.  
    15.     public void left(){
    16.    
    17.      rb.AddForce(dk * Time.deltaTime, 0, 0,ForceMode.VelocityChange);
    18.    
    19.     }
    20.    
    21.     public void right(){
    22.    
    23.     rb.AddForce(-dk * Time.deltaTime, 0, 0,ForceMode.VelocityChange);
    24.    
    25.     }
    26.    
    27.    
    28.    
    29. }
    to use it attach this script to your player or whatever you wanna move and make two buttons by going to Game Object > UI > Button and place them according your comfort and click on them to open inspector panel...now down below you'll see Written "OnClick()" and some space and + - Sign...click on + sign and now drop your player on whom you placed the script into the field in "OnClick()" and in Next...where u see "No Function" In OnClick(),click on it and hover over move script and select "right()" for moving right and "left()" for moving left using that button....now do this on both buttons and test it out..and let me know if it worked or not!
     
    AmbarishGK likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just for another option, the 'Touch' has a position in pixels coordinates. The Screen.width (https://docs.unity3d.com/ScriptReference/Screen-width.html) is the number of pixels wide.

    So, with those 2 pieces of information, you can determine if it's left or right on the screen and act accordingly. :)
     
    AmbarishGK likes this.
  4. AmbarishGK

    AmbarishGK

    Joined:
    Dec 30, 2017
    Posts:
    3
    Thanks, the second method worked:):)