Quaternion.rotation and .localRotation behaving identical

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?

Transform.localRotation uses the object’s parent Transform’s axes for the rotation. I think what you want is to use Transform.Rotate() using the Space.World value for the relativeTo argument.

Transform.Rotate() requires a Vector3, doesn’t it? I am hoping that this can be done with converting Quaternions into Vector3s, etc.

you should also cache the GoT rotation when press is false, and you’re calculating Relative and the goT.rotation backwards.

        if (buttonPressTest == false)
        {
            //Set trackedObjectQF to the current rotation of the tracked object
            trackedObjectQF = trackedObjectT.rotation;
            goTQF = goT.rotation;
        }
     
        if (buttonPressTest == true)
        {
            trackedObjectQT = trackedObjectT.rotation;

            Quaternion relative = trackedObjectQT * Quaternion.Inverse(trackedObjectQF);

            //Calculate the changes in rotation that has happened after button press and apply them to go
            goT.rotation = goTQF * relative;
        }

recalculating from the cached rotation every frame will prevent accumulation of floating precision loss.
instead of inverse(fromRotation) * currentRotation, use currentRotation * Inverse(FromRotation) which will give you the difference of (current - from), aka the delta rotation.
finally. goTFromRotation * relative (aka starting rotation * delta rotation) will give you the offset that goT needs to be rotated to as its updated.

Thanks for the suggestions! Unfortunately, the behavior remains the same. Rotations are synched using the local axis instead of the global axis.