rotation with Quaternions and Raycasthit

Hello Guys

I have a trouble: I have my model in the scene and I would like to apply an interactive rotation using Quaternions, on my IPad. So, I touch the screen and I save the current position/rotation of my object in StartQuat. Then I start moving my finger on the screen but using Slerp of the quaternion, makes my object rotating towards my finger and then it follows it. What I would like to do is NOT having the first quick rotation towards my finger, and wherever I touch the screen, I need to apply the rotation at the current position. I think I’m almost there but for whatever reason I can’t find the bug.
Thank you for any help! :slight_smile:
This is the code:

function LateUpdate() {

	var StartRay : Ray;
	var TargetRay: Ray;
	var hit: RaycastHit;
	var targetHit: RaycastHit;
	var angleBetweenVectors: float;
	var endQuat: Quaternion;
	
	if(Input.GetTouch(0).phase == TouchPhase.Began) { // I grab the initial position of the transform.
		StartRay = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
		if(Physics.Raycast(StartRay,hit,100)) { //If the finger hit the object's collider I grab the object's position
				StartQuat = hit.transform.rotation;
				TargetQuat = StartQuat;
		}
	}
	
	if(Input.GetTouch(0).phase == TouchPhase.Moved) {
		TargetRay = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
	 	angleBetweenVectors = Vector3.Angle(StartRay.GetPoint(100), TargetRay.GetPoint(100));
	
		
		if(Physics.Raycast(TargetRay, targetHit, 100)) {
			endQuat =  Quaternion.SetFromToRotation(StartRay.GetPoint(100), TargetRay.GetPoint(100));	
			
			targetHit.transform.rotation = endQuat;
			
		}
		
	}
	
}

You might need to rethink your logic.
Are you expecting to be able to rotate the object over 180 degrees?
Or are you trying to make the object ‘LookAt’ your finger?

This is what I am trying to do:

  1. I touch the screen.
  2. I move my finger and a rotation is applied. The angle between the first ray (at the touch) casted in the scene and the further rays (casted in the movements) defines the rotation and it’s direction.
  3. I lift up my finger and I save the transform.rotation.

if I touch the screen again, I don’t want my object rotating towards my finger again as it would null the previous one. But for what ever reason, it’s what is happening. :frowning:

It’s really just like a trackball and the mouse… but I can’t see the bug. :frowning:

I should be able to rotate the object over 360.
GC

the bug is that you are setting the transform’s rotation to Quaternion.SetFromToRotation(StartRay.GetPoint(100), TargetRay.GetPoint(100));
which is almost 0 in the beginning.
To make it incremental, you have to multiply by the quaternion that is created based on the position of the finger in the current frame and the position of the finger in the last frame.
so it would be something like:

targetHit.transform.rotation *=  Quaternion.SetFromToRotation(PrevFrameRay.GetPoint(100), CurrFrameRay.GetPoint(100));
PrevFrameRay = CurrFrameRay;

I am not sure why you chose to use rays 100 units long, but whatever. If it gives you the behaviour that you want.

Uhm… well 100 is used everywhere so I thought to use it… I used 10, 3, but nothing changed. Your correction tho, was very helpful… and it works… :slight_smile:

Although I am experimenting now the use of Slerp… which is causing the same troubles as before. O.o

so I might go back to the previous version eventually.