So I am trying to make it so a player can pick up an object and rotate it using the mouse. I have the picking up part done, but when I try to make it rotate the object I am having a hard time rotating it properly. What I think my code should do it rotate it towards the players face because the axis of rotation is the player’s right. The first time I rotate it after starting the game, it works perfectly every time. But after I rotate it once, it messes up the rotation and it no longer rotates along the axis.
My current code chunks:
Quaternion quatRotangle(Vector3 inVec, float a) {
Quaternion outQuat;
float quatMag;
// Here we calculate the sin( theta / 2) once for optimization
float result = Mathf.Sin( a / 2.0f );
// Calculate the x, y and z of the quaternion
float x = inVec.x * result;
float y = inVec.y * result;
float z = inVec.z * result;
// Calcualte the w value by cos( theta / 2 )
float w = Mathf.Cos( a / 2.0f );
quatMag = Mathf.Sqrt (x*x+y*y+z*z+w*w);
outQuat = new Quaternion(x/quatMag, y/quatMag, z/quatMag, w/quatMag);
//outQuat = outQuat.normalize();
return outQuat;
}
and to rotate towards the player
rot = rot*quatRotangle(cam.transform.right,Input.GetAxis("Mouse Y"));
tar.transform.rotation = rot;