Rotate object 360 relative to camera

Ok, I’m scratching my head about this.

I’m trying to rotate an object on X and Y axis relative to camera, but the Z relative to the world.

It’s a VR prototype, but the problem is not exclusive to VR. So I want the XY be the motion controller relative to the player rotation. For example: If the player tilt row (Y) while looking north, the object will rotate the Y axis. If looking east, will rotate X axis, and so on.

And the Z always fixed.

The problem is, when reach 90 degrees, the rotation goes crazy. Looks like it’s a problem with quaternion, which I didn’t understand nothing. lmao

I’m out of ideas. Anyone knows how to fix that?

What I’m trying to do:

The best solution I found but still the rotation go crazy: