I'm having a problem with the rotation of a game object. I'm able to rotate the object in the direction/angle of another game object. But after the first rotation the game object i'm rotating based on the delta rotation of the source game object It starts rotating in a different direction because it has been rotated once already I guess.
So is there a way so that after the first rotation I can reset the rotation so It moves again in the angle/direction the other object is rotating?
This is my current code below, i'm not a math expert so I might be doing things wrong .. So the basic thing I want is that the second game object rotates in the same direction the base game object rotates but I don't want the rotation values to be equal, the rotation values doesn't have to be the same, it only has to rotate in the same angle/direction as the base game object rotates. TiltAgainstObject is the source game object. The script is attached to the second game object.
Any help is welcome :).
public class Tilt : MonoBehaviour {
public GameObject TiltAgainstObject;
private float rotX;
private float rotY;
private float rotZ;
private float rotW;
private float deltaX;
private float deltaY;
private float deltaZ;
private float deltaW;
// Use this for initialization
void Start () {
rotX = TiltAgainstObject.transform.rotation.x;
rotY = TiltAgainstObject.transform.rotation.y;
rotZ = TiltAgainstObject.transform.rotation.z;
rotW = TiltAgainstObject.transform.rotation.w;
}
// Update is called once per frame
void Update () {
if(OVRInput.Get(OVRInput.Button.One))
{
deltaX = TiltAgainstObject.transform.rotation.x - rotX;
deltaY = TiltAgainstObject.transform.rotation.y - rotY;
deltaZ = TiltAgainstObject.transform.rotation.z - rotZ;
deltaW = TiltAgainstObject.transform.rotation.w - rotW;
gameObject.transform.localRotation = new Quaternion(gameObject.transform.rotation.x + deltaX,
gameObject.transform.rotation.y + deltaY,
gameObject.transform.rotation.z + deltaZ,
gameObject.transform.rotation.w + deltaW);
}
rotX = TiltAgainstObject.transform.rotation.x;
rotY = TiltAgainstObject.transform.rotation.y;
rotZ = TiltAgainstObject.transform.rotation.z;
rotW = TiltAgainstObject.transform.rotation.w;
}
}