# How do I rotate Z axis based off mouse click and drag?

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;
}
}
}
``````

You are multiplying speed by Time.time in the drag - that’s not a good idea. Slerp wants a number between 0 and 1, but Time.time is the number of seconds since the game started.

Your best choice would be to replace that with something like speed/10 which will incrementally move the rotation towards the target each frame by a percentage of the difference which will give a smoothed effect.