Search Unity

Touch: move left and right

Discussion in '2D' started by ArtySBC, May 23, 2016.

  1. ArtySBC

    ArtySBC

    Joined:
    Sep 6, 2015
    Posts:
    3
    Hello everyone,

    I'm developing a simple mobile game where you have to move the object left and right by tapping or holding one of the two halves of the screen but i don't know how to do that, I didn't found out any tutorials that explain exactly what I'm looking for. Can you help me please?
    Thanks :)
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    There are tons of examples of this across the internet, so you might want to work on your web searching skills.

    You can start with checking out the documentation for Touch input events.

    If you want to know if any touches are currently happening, you can check Input.touchCount, which tells you the number of touches happening.
    http://docs.unity3d.com/ScriptReference/Input-touchCount.html

    You can get info about a specific touch by using Input.GetTouch(0). The zero means the first touch, if you wanted the second touch you would give one as the parameter.
    http://docs.unity3d.com/ScriptReference/Input.GetTouch.html
    http://docs.unity3d.com/ScriptReference/Touch.html

    Then to see what the touch is doing this frame, you can use TouchPhase.
    http://docs.unity3d.com/ScriptReference/TouchPhase.html

    So here's an example of getting the touch and checking which side of the screen its on.
    Code (CSharp):
    1. using UnityEngine;
    2. public class HalfScreenTouchMovement : MonoBehaviour
    3. {
    4.     private float screenCenterX;
    5.  
    6.     private void Start()
    7.     {
    8.         // save the horizontal center of the screen
    9.         screenCenterX = Screen.width * 0.5f;
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         // if there are any touches currently
    15.         if(Input.touchCount > 0)
    16.         {
    17.             // get the first one
    18.             Touch firstTouch = Input.GetTouch(0);
    19.  
    20.             // if it began this frame
    21.             if(firstTouch.phase == TouchPhase.Began)
    22.             {
    23.                 if(firstTouch.position.x > screenCenterX)
    24.                 {
    25.                     // if the touch position is to the right of center
    26.                     // move right
    27.                 }
    28.                 else if(firstTouch.position.x < screenCenterX)
    29.                 {
    30.                     // if the touch position is to the left of center
    31.                     // move left
    32.                 }
    33.             }
    34.         }
    35.     }
    36. }
     
    Last edited: May 23, 2016
    Powzone, atulvi, Shivansh_ and 2 others like this.
  3. za5620485

    za5620485

    Joined:
    Mar 14, 2020
    Posts:
    3

    Bro your code only bi right touch not to left...i try this i touch left but player go to right...
     
    Valkiriyana likes this.
  4. changstevenn

    changstevenn

    Joined:
    Jun 9, 2020
    Posts:
    2
    the postion.x , isit in the mid? i only can go 1 side
     
    Valkiriyana likes this.