I am trying to have one object rotate based on the rotation of a second object, but only when that object is “activated” by a button. (Specifically, have an object rotated when one of the buttons on an HTC Vive controller is pressed.)
In an effort to test the code before worrying about an external controller, I wrote the following with a Boolean as the “button”:
public GameObject trackedObject;
public bool buttonPressTest;
private GameObject go;
private Transform goT;
private Transform trackedObjectT;
private Quaternion trackedObjectQF;
private Quaternion trackedObjectQT;
// Use this for initialization
void Start ()
{
//Define go as the object the script is attached to and get its Transform
go = this.gameObject;
goT = go.GetComponent<Transform>();
//Get the tracked object's transform
trackedObjectT = trackedObject.GetComponent<Transform>();
}
// Update is called once per frame
void Update ()
{
if (buttonPressTest == false)
{
//Set trackedObjectQF to the current rotation of the tracked object
trackedObjectQF = trackedObjectT.rotation;
}
if (buttonPressTest == true)
{
trackedObjectQT = trackedObjectT.rotation;
//Debug.Log(trackedObjectQT);
Quaternion relative = Quaternion.Inverse(trackedObjectQF) * trackedObjectQT;
//Debug.Log(relative);
//Calculate the changes in rotation that has happened after button press and apply them to go
goT.rotation = relative * goT.rotation;
//Update trackedObjectQF to the current rotation of the tracked object
trackedObjectQF = trackedObjectT.rotation;
//Debug.Log(trackedObjectQF);
}
}
However, all of the rotations are behaving as if they are local, not global. For example, a rotation about an object’s local x-axis rotates the second object about its x-axis, instead of the rotations taking place about a global axis. Any suggestion for how to make the rotations correspond to global axes?