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!
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?
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.
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.
It’s really just like a trackball and the mouse… but I can’t see the bug.
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: