TouchScreen

Dear Sir/Madam
i got a problem with “how to control my game to move up,down,left and right” .The first I control my moving game with button key(left,right,up and down).But I want to control my moving game with touch on screen.so how can i solve it? please see my code to control on Key Button.
+++++++++++++++++++++++++++++++Code+++++++++++++++++++++

public void playerMove()
{

    GameObject Moverment = GameObject.Find("Player");
    float moveHorizontal = Input.GetAxis("Horizontal");

    float moveVertical = Input.GetAxis("Vertical");

   Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
   Moverment.rigidbody.velocity = movement * SingletonManage.Instance.gameData_Ref.speed;
   Moverment.rigidbody.position = new Vector3
  (
      Mathf.Clamp(Moverment.rigidbody.position.x, SingletonManage.Instance.gameData_Ref.xMin, SingletonManage.Instance.gameData_Ref.xMax),
      7f,
      Mathf.Clamp(Moverment.rigidbody.position.z, SingletonManage.Instance.gameData_Ref.zMin, SingletonManage.Instance.gameData_Ref.zMax)
  );

    Moverment.rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, Moverment.rigidbody.velocity.x *  -(SingletonManage.Instance.gameData_Ref.tilt));
}

Simple way: create GUITexture as joystick for touch. Than (write on CSharp):

 public GUITexture myMoveBtn = null; //change, reference on your gui texture
 private int finId1 = -1; //id finger for cancel touch event

 private GameObject Moverment = null;
 //UPDATE 1: use global variable for your player object
 void Start() {
  //Only one time find your game object
  Moverment = GameObject.Find("Player");
 }

 void Update() {
  //Check count of touches
  if(Input.touchCount > 0) {
   foreach (Touch touch in Input.touches) { 
    if (touch.phase  == TouchPhase.Began && myMoveBtn.HitTest(touch.position) && finId1 == -1) {
     finId1 = touch.fingerId; //store Id finger
     //Do stuff
    }
    //Move
    if(touch.fingerId == finId1) {
     //Calc right angle
     //UPDATE 1: using center of GUITexture
     float ang = Mathf.Atan2(touch.position.x - myMoveBtn.GetScreenRect().center.x, touch.position.y - myMoveBtn.GetScreenRect().center.y)
     //your code
     float moveHorizontal = 1.0f * Mathf.Sin(ang);
     float moveVertical = 1.0f * Mathf.Cos(ang);
     //An etc your code, where you calculate move
     //...
    }
    if (touch.phase == TouchPhase.Ended) { //correct end of touches
     if(touch.fingerId == finId1) { //check id finger for end touch
      finId1 = -1;
      //UPDATE 1: stop rigidbody, when you release GUITexture
      Moverment.rigidbody.velocity = new Vector3(0, 0, 0); //you can use "Vector3.zero" too.
      Moverment.rigidbody.angularVelocity = Vector3.zero;
     }
    }
   }
  }
 }

I hope that it will help you.

P.S.: Realy touch checks only real device or Eclipse emulator of android (AVD).

Hello! You can use “Imput.TouchCont” and “Imput.GetTouch (int Touch)” to work with touchs. Within the GetTouch, you can take the position of the touch (Imput.GetTouch (0) .position.x, for example). :slight_smile:

The easiest way to implement touch controls for newbies like us is the asset “easy touch controls” in the asset store for 10$. I am new to unity and programming and this asset worked for me without any problem. that’s very easy.