I am attempting to rotate an object and it is just spinning out of control. I know it boils down to something with my math not being correct but I’m still trying to wrap my head around the whole Vector/Quaternion thing.
Any ideas as to why it is spinning so out of control. Also any suggestions on good resources to better understand Vectors and Quaternion?
public class SpinWithMouse : MonoBehaviour
{
public Transform target;
public float speed = 1f;
Transform mTrans;
Vector3 startPoint;
void Start ()
{
mTrans = transform;
}
void OnPress()
{
Vector3 currentLoc = UICamera.currentTouch.pos;
startPoint = (currentLoc - transform.localPosition);
}
void OnDrag (Vector2 delta)
{
Vector3 newLoc = UICamera.currentTouch.pos;
Vector3 targetPoint = (newLoc - transform.localPosition);
Quaternion targetRotation = Quaternion.LookRotation(newLoc - startPoint);
if (target != null)
{
target.localRotation = Quaternion.Slerp(target.localRotation, targetRotation, speed) * target.localRotation;
}
else
{
mTrans.localRotation = Quaternion.Slerp(mTrans.localRotation, targetRotation, speed) * mTrans.localRotation;
}
}
}