Log in or register to post comments

Can't rotate by touch an object in augmented reality

May 18, 2012 - 10:27am #4

I need to rotate by touch an object attached to the image target when it appears on screen in a AR project. I have used this script http://www.revelopment.co.uk/tutorials/unitytutorials/73-howtorotateanobjectbytouch in others projects but I don´t know why doesn't work with this one.

I'm using the Vuforia extension for Unity version 1.5.10 in unity 3.5

the code:

// Revelopment.co.uk
// Created by Carlos Revelo
// 2011




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

var targetItem : GameObject;
var GUICamera : Camera;
var ambient : GameObject;


/********Rotation Variables*********/
var rotationRate : float = 1.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.5f;

private var itemInertiaDuration : float = 1.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);		
         				
         	}	
        
        
      }	
}

Re: Can't rotate by touch an object in augmented reality

May 18, 2012 - 10:59am #3

Didn't tried the script, but I think you should check the layermask, it might be the problem.

Can't rotate by touch an object in augmented reality

May 18, 2013 - 5:10am #2

The language that you use there is Javascript while the language used by Vuforia is C# in Unity. I think that's the problem.

Can't rotate by touch an object in augmented reality

March 2, 2014 - 3:13am #1

Hello allx2000, did you find solution

Log in or register to post comments