- Home /
Quaternions; how do I rotate an object around only 2 axis based on another rotation?
Cross posted from the forums:
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; as if it were rotating on a turntable.
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've also tried LookRotation and SetLookRotation, but could not get those to work either.
i would start by making the character object the parent of the camera and take it from there.
another words, drag and drop the camera object on to the character object in the hierarchy in the inspector. this way it is "stuck" to the character.
you can still easily adjust any of you other custom movements after parented.
Answer by JoshuaMcKenzie · Feb 10, 2016 at 04:17 AM
if you want to rotate by only the xz axis you can follow these steps:
find the direction between the camera and the skyscraper
zero the y value out
apply it to the Skyscraper's Look rotation
// find the direction Vector3 direction = Camera.main.transform.position - transform.position; //zero out the y value direction.y = 0; // set the skyscraper's look rotation to this value transform.rotation = Quaternion.LookRotation(direction,Vector3.Up);
Your answer
Follow this Question
Related Questions
need my object to rotate between specific angles on the x axis 2 Answers
Prevent child rotation but still control it yourself 1 Answer
Rotating 2D sprite to match surface. 0 Answers
Problem when i rotate a 2d object aroun Z-axis 1 Answer
How do you make a Tps cam with RotateAround around the X and Y axis without rotating on the Z axis? 1 Answer