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

Input in android

Discussion in 'Android' started by abdoubelbala, Feb 24, 2015.

  1. abdoubelbala

    abdoubelbala

    Joined:
    Aug 4, 2012
    Posts:
    51
    Hi guys
    can any one here tell me what to do , i'am good with 2D in unity ,
    i want to build my game to android ,the problem here is i don't have any idea about input in android ,
    per example i have a spaceshotter game, and i want the spaceship move when the player hit the screen,
    any help
    please.
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    if you have ship with rigidbody2D
    Code (CSharp):
    1. public Vector2 shipPosition, touchPosition, shipDirection;
    2.     void Update () {
    3.         if (Input.touchCount > 0)
    4.         {Touch touch = Input.GetTouch(0);
    5.             if (touch.phase == TouchPhase.Began){
    6.                 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    7.                 shipDirection = touchPosition - shipPosition;
    8.                 rigidbody2D.velocity = new Vector2 (shipDirection.x, shipDirection.y );
    9.             }
    10.         }
    11.     }
    its not realy good example, but you can make it better for your game :)

    or check tutorial for touch controll

    http://pixelnest.io/tutorials/unity-touch-controls/
     
    abdoubelbala likes this.
  3. abdoubelbala

    abdoubelbala

    Joined:
    Aug 4, 2012
    Posts:
    51
    Thank you i really appreciate it