Log in or register to post comments

Help...how to rotate objects with the touch

October 17, 2011 - 2:03am #1

Hi Guys,

I hope you can help me with this: I created a script file attached to each object. When each object pops up, I would like to be able to rotate it when I touch the IPad2 screen. I have created another program with the attached script but with no augmented reality, and it works but when I add the augmented reality libs it doesn't work anymore :(
Many Thanks for the help.

GC.

This is the script:

public var amountToInterpolate: float = 0.5f;
private var startQuat: Quaternion; 
private var targetQuat : Quaternion ;
private var offSetQuat:Quaternion ;
private var lastRot:Quaternion;
private var objectHit: boolean;



function Start() {

	//lastRot = transform.rotation;
	lastRot = transform.localRotation;

}


function ShootRay ()
 
{
 
		var ray: Ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
 
		var tempQuat : Quaternion = new Quaternion();
 
		//var hit: RaycastHit;
		var rhit: RaycastHit;
 
		if (Physics.Raycast (ray, rhit, 1000.0f)) {
  			objectHit =  true;
  			
		}
		
		Debug.Log("ObjectHit: " + objectHit);
		if(objectHit) {
			tempQuat.x = ray.GetPoint(10).x;//rhit.point.x;
 			tempQuat.y = ray.GetPoint(10).y;//tempQuat.y = rhit.point.y;
 			tempQuat.z = ray.GetPoint(10).z;//tempQuat.z = rhit.point.z;
 			 
		}
 
		return tempQuat;
 
}
 

function Update () {


//This is for the MouseDown
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {

		if(objectHit) 	
			startQuat = transform.localRotation;//rotation;
			
		offSetQuat = ShootRay() * startQuat;
 
}

//This is for MouseUp
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
	lastRot = transform.localRotation;//rotation;
	objectHit = false;
}

//This is for the MouseDrag
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
		
		if(objectHit) {
			Debug.Log("Dragging ");
			Debug.Log("objectHIT " + objectHit);
			targetQuat = ShootRay() * offSetQuat;
			
			var endQuat: Quaternion = Quaternion.Slerp (startQuat, targetQuat, Time.time * amountToInterpolate);
			
			//transform.rotation = endQuat;
			transform.localRotation = endQuat;
 			startQuat = targetQuat;
 			Debug.Log("name: " + transform.gameObject.name);
 		}
}


}	


Re: Help...how to rotate objects with the touch

October 18, 2011 - 6:13am #7

Hi,

thank you for that !! It worked also with the RayCast.. but still I don't understand why my version doesn't work :(. My script is attached to each object and I ray cast the position of the touch as they do. Moreover, I compute the quaternion and use the .localRotation which is the same as using the Rotate(....., Space.self). so I don't really get....
would you be so kind if you can use it and see if it works with u?

Many thanks.
GC.:)

public var amountToInterpolate: float = 0.5f;
private var startQuat: Quaternion; 
private var targetQuat : Quaternion ;
private var offSetQuat:Quaternion ;
private var lastRot:Quaternion;
private var objectHit: boolean = false;
private var objHit: RaycastHit;

function Start() {

	lastRot = transform.localRotation;
}


function ShootRay (p:Vector3)
 
{
 		var ray: Ray = Camera.main.ScreenPointToRay (p);
 
		var tempQuat : Quaternion = new Quaternion();
		var rhit: RaycastHit;
 
		if (Physics.Raycast (ray, rhit, 1000.0f)) {
 			Debug.Log("Ray Shot and hit!");
 			objectHit =  true;
 			objHit = rhit;
		}
		
		if(objectHit) {
			tempQuat.x = ray.GetPoint(10).x;//rhit.point.x;
 			tempQuat.y = ray.GetPoint(10).y;//tempQuat.y = rhit.point.y;
 			tempQuat.z = ray.GetPoint(10).z;//tempQuat.z = rhit.point.z;
		}
 
		return tempQuat;
 
}
 




function FixedUpdate () {

var theTouch : Touch;

if(Input.touchCount > 0)
	theTouch = Input.GetTouch(0);
	
	
//This is for the MouseDown
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
		
		if(objectHit == false) {
			Debug.Log("Begin Touch");
			startQuat = lastRot;
			offSetQuat = ShootRay(theTouch.position) * startQuat;
		}
		
}

//This is for MouseUp
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {

	if(objectHit)
		lastRot = transform.localRotation;
	
	Debug.Log("End Touch");
	objectHit = false;
}

//This is for the MouseDrag
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
		
		if(objectHit) {	
			Debug.Log("Move Touch");	
			targetQuat = ShootRay(theTouch.position) * offSetQuat;
			var endQuat: Quaternion = Quaternion.Slerp (startQuat, targetQuat, Time.time * amountToInterpolate);
			transform.localRotation = endQuat;
 			startQuat = targetQuat;			
 		}
}



}	


Re: Help...how to rotate objects with the touch

October 18, 2011 - 3:05am #6

watch the video in tutorial , then assign the targetItem to the Object you wanna rotate , and it should work , if it didn't , comment out the raycasting condition

Re: Help...how to rotate objects with the touch

October 17, 2011 - 3:11am #5

Hi Mr.

thank you for your help, I just have a last question if I can.
Once you computed the rotation, did you assigned it to the localRotation field of the selected game object or to the Rotation.

Cheers,
G.

Re: Help...how to rotate objects with the touch

October 17, 2011 - 2:57am #4

yes it worked with QCAR too . but I had to comment out the raycasting condition to make it work(with or without AR camera)

Re: Help...how to rotate objects with the touch

October 17, 2011 - 2:27am #3

HI Thank you for your Reply,

I'm sure that will help. Just a question... were you successful in using that code with the AR camera by the Qualcomm Toolkit?.
Actually my code works really well if I don't use the AR Camera of the qualcomm toolkit but for whatever reason it stops working when I add my code into an AR application :(

Many Thanks,
GC.

Re: Help...how to rotate objects with the touch

October 17, 2011 - 2:21am #2

I wanted to do the same things a few days ago and found this very useful tutorial :
http://revelopment.co.uk/tutorials/unitytutorials/73-howtorotateanobjectbytouch

Log in or register to post comments