miércoles, 1 de agosto de 2012

Girar objeto al tocarlo en Uniti 3D (Rotate a game object by toch in Unity 3D)

En este tutorial se explica como girar un objeto cuando se toque y arrastre en la pantalla de un dispositivo móvil con sistema operativo iOs y Android.


1. Agregar un colisionador al objeto que se desea rotar.


Se selecciona el objeto y en el menú Component -> Physics -> Mesh Colider
(Se puede utilizar cualquier opción precedida por "Colider" según se requiera)  


2. Establecer el tag "Main Camera" a la cámara.


Se selecciona la cámara principal y en el inspector se cambia la opción Tag y se selecciona Main Camera.





3. Incluir el Script que realizara la función.


Se adjunta el siguiente script al objeto que se desea rotar. (esta realizado en JavaScript)


// Revelopment.co.uk

// Created by Carlos Revelo

// 2011

/********Main Objects***********/



var targetItem : GameObject;

var GUICamera : Camera;

var ambient : GameObject;





/********Rotation Variables*********/

var rotationRate : float = 0.0;

private var wasRotating;



/************Scrolling inertia variables************/

private var scrollPosition : Vector2 = Vector2.zero;

private var scrollVelocity : float = 0;

private var timeTouchPhaseEnded: float;

private var inertiaDuration : float = 0.0f;



private var itemInertiaDuration : float = 0.0f;

private var itemTimeTouchPhaseEnded: float;

private var rotateVelocityX : float = 0;

private var rotateVelocityY : float = 0;



var hit: RaycastHit;



private var layerMask = (1 <<  8) | (1 << 2);



function Start()

{

     layerMask =~ layerMask;    

}



function FixedUpdate()

{

    

     if (Input.touchCount > 0)

     {          //     If there are touches...

               var theTouch : Touch = Input.GetTouch (0);          //     Cache Touch (0)

              

               var ray = Camera.main.ScreenPointToRay(theTouch.position);

               //var GUIRay = GUICamera.ScreenPointToRay(theTouch.position);

              

                   

              if(Physics.Raycast(ray,hit,50,layerMask))

              {    



                                               if(Input.touchCount == 1)

                              {

                                  

                                   if (theTouch.phase == TouchPhase.Began)

                                  {

                                       wasRotating = false;    

                                  }         

                                 

                                  if (theTouch.phase == TouchPhase.Moved)

                                  {

                                     

                                       targetItem.transform.Rotate(theTouch.deltaPosition.y * rotationRate, -theTouch.deltaPosition.x * rotationRate,0,Space.World);

                                       wasRotating = true;

                                  }         

             

                                  if (theTouch.phase == TouchPhase.Ended || theTouch.phase == TouchPhase.Canceled)

                                  {

                                       if(wasRotating==true)

                                       {

                                            if(Mathf.Abs(theTouch.deltaPosition.x) >=10)

                                            {

                                                 rotateVelocityX = theTouch.deltaPosition.x / theTouch.deltaTime;

                                              }

                                              if(Mathf.Abs(theTouch.deltaPosition.y) >=10)

                                            {

                                                 rotateVelocityY = theTouch.deltaPosition.y / theTouch.deltaTime;

                                              }    

                                       itemTimeTouchPhaseEnded = Time.time;

                                       }

                                                   }

                              }

               }                         

              

     }

/******************** Inertia code ******************************/

    if(Input.touchCount == 0 )

    {

                if(scrollVelocity != 0.0)

              {

                   //slowing down

                   var t : float = (Time.time - timeTouchPhaseEnded) / inertiaDuration;

                   var frameVelocity : float = Mathf.Lerp(scrollVelocity, 0 , t);

                            

                   scrollPosition.x -= frameVelocity * Time.deltaTime;

                            

                   if(t >= inertiaDuration)

                        scrollVelocity = 0.0f;

                            

                            

              }    

        

                if(rotateVelocityX != 0.0f || rotateVelocityY != 0.0f)

              {

                   //slowing down

                   var ty : float = (Time.time - itemTimeTouchPhaseEnded) / itemInertiaDuration;

                   var XVelocity : float = Mathf.Lerp(rotateVelocityX, 0 , ty);

                   var YVelocity : float = Mathf.Lerp(rotateVelocityY, 0 , ty);     

                  

                            

                   if(ty >= inertiaDuration)

                   {

                        rotateVelocityX = 0.0f;

                        rotateVelocityY = 0.0f;

                       

                   }    

                   targetItem.transform.Rotate(YVelocity*Time.deltaTime * rotationRate, -XVelocity * Time.deltaTime * rotationRate,0,Space.World);         

                            

              }    

       

       

      }    

}



4. Ajustes.


Se selecciona el objeto y en el inspector en el componente del script se arrastra desde la pestaña Hierarchy el objeto que se rotara (Que es el mismo que se tiene seleccionado) a la variable Target Item.



A la variable Rotation Rate se cambia el valor de 0 a 1. (Este valor indica los grados que rotara el objeto respecto al movimiento del dedo en la pantalla, este valor se puede aumentar o reducir según se desee ya que la sensibilidad al tacto puede variar en cada dispositivo móvil)




Nota


El script no lo cree yo se puede encontrar en el sitio www.revelopment.co.uk