So I have a camera that rotates around an object, always facing it. What I would like to do now is to have that object rotate with the camera, but only along the xz plane (in other words; around the y axis).
Imagine a skyscraper with a camera rotating around it in all directions, always facing it. I want the skyscraper to always face the camera, but not leave the ground.
I don’t have much code so far, but this is what I have tried:
Quaternion difference = Quaternion.Inverse(cameraRigging.rotation) * masterCamera.transform.rotation;
// rotate camera reletive to main camera
cameraRigging.rotation = masterCamera.transform.rotation;
// rotate the object such that it is still facing the
// same direction reletivly to the camera along z and
// x axes (NOT WORKING)
target.transform.rotation *= difference;
This isn’t really working; the rotation doesn’t stay on Y (as expected), but it also is not rotating the way I would expect. Target almost faces the same direction as camera, but not quite.
I’m not entirely positive what you’re asking here, but I’m assuming you want the skyscraper to face the camera while having it’s Up vector perpendicular to the X and Z vectors. There’s a couple ways to do this, but a fairly simple one is to take the direction from the position of your skyscraper to the camera and project it onto the ZX plane.
Vector3 direction = masterCamera.transform.position - target.transform.position;
direction = Vector3.ProjectOnPlane(direction, Vector3.up);
target.transform.rotation = Quaternion.LookRotation(direction);
This will set the target’s rotation to point in the direction of it’s local Z, the direction being a Vector pointing at the mainCamera but laying flat on the ZX plane. I think this is what you’re after.