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. Dismiss Notice

2D horizontal ball movement

Discussion in '2D' started by Nvent97, Oct 3, 2015.

  1. Nvent97

    Nvent97

    Joined:
    Oct 3, 2015
    Posts:
    1
    Hello, this is my first post so I hope you can help with my problem.
    Im making an android game, and I want to move a ball after you touch the devices screen. For instance,
    if you want to move the ball to the right you have to touch the right side of the screen and the same to the left.
    I would be realy thanksful if you help me with that.

    Im sorry for my english because im from Argentina.

    Thank you.
     
  2. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    So you problem is detect where user touch screen?
    You can try this:
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         // Mouse
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             if (Input.mousePosition.x >= Screen.width / 2)
    7.                 Debug.Log("Right");
    8.             else
    9.                 Debug.Log("Left");
    10.         }
    11.  
    12.         // Touch
    13.         if (Input.touchCount == 1)
    14.         {
    15.             Touch currentTouch = Input.GetTouch(0);
    16.  
    17.             if (currentTouch.phase == TouchPhase.Began)
    18.             {
    19.                 if (currentTouch.position.x >= Screen.width / 2)
    20.                     Debug.Log("Right");
    21.                 else
    22.                     Debug.Log("Left");
    23.             }
    24.         }
    25.     }