Search Unity

Android Unity Tutorial

Discussion in 'Editor & General Support' started by rasarahul, Jan 19, 2011.

  1. rasarahul

    rasarahul

    Joined:
    Jan 19, 2011
    Posts:
    12
    Can anyone send me a link for android unity tutorial for moving game object by touching it,this is the code i am working on..i dont know what is the problem????
    Code (csharp):
    1. var object:GameObject;
    2. private var selected:boolean=false;
    3. function Update () {
    4. for(var i=0; i<Input.touchCount; i++)
    5. {
    6.   var ray=Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
    7.   var hit: RaycastHit;
    8.   if(Input.GetTouch(i).phase==TouchPhase.Began)
    9.   {
    10.     if(Physics.Raycast(ray,hit))
    11.     {
    12.       if(hit.collider.gameObject==object)
    13.       {
    14.          selected=true;
    15.       }
    16.     }  
    17.   }
    18.   else if(Input.GetTouch(i).phase==TouchPhase.Moved){
    19.     if(selected==true){
    20.       var CameraTransform=Camera.main.transform.InverseTransformPoint(0,0,0);
    21.       var touchPosition= Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x,Input.GetTouch(i).position.y,CameraTransform.z));
    22.       object.transform.position=touchPosition;
    23.       object.transform.position.y=1;
    24.    
    25.     }
    26.     else if(Input.GetTouch(i).phase==TouchPhase.Ended){
    27.      selected=false;
    28.     }
    29.   }
    30. }
    31. }
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There is a code example attached to this thread which demonstrates how to do this. It was originally developed for iOS but should work OK with Android.
     
  3. rasarahul

    rasarahul

    Joined:
    Jan 19, 2011
    Posts:
    12
    Thank u andee,could you please tell me how to display some message when a game object is touched in android????