- Home /
Rotate an object's yaw, pitch, and roll relative to the Game Camera Axis.
Hi Everyone!
I'm currently trying to write a script to rotate an object relative to the camera's reference axis. As of now in the script below, objects are rotated relative to themselves. I would like to change this, see below (this is a script attached to the gameobject to be rotated which references a control script and the camera transform).
void Rotate(float deg)
{
// Make sure the camera exists
var camera = GetCamera(Camera, gameObject);
if (camera != null)
{
int rotMode =obj.GetComponent<AssetControl>().RotationMode;
var axis = new Vector3(0, 0, 0);
if (rotMode == YAW) {
axis = new Vector3(0, 1, 0); //rotate about the y axis
} else if (rotMode == ROLL) {
axis = new Vector3(1, 0, 0); //rotate about the x axis
} else if (rotMode == PITCH) {
axis = new Vector3(0, 0, 1); //rotate about the z axis
}
transform.rotation *= Quaternion.AngleAxis(deg, axis); //do rotation
}
}
Ideally I would like to rotate the object relative to the camera orientation, i.e. if rotating by ROLL, rotate the the object about the axis pointing the the camera.x direction, rather than the x axis of the global world. In a nut shell, I'm trying to replace the game objects coordinate frame (x,y,z arrows), with the cameras coordinate frame, and rotate the about about itself. Basically there's no translation, and I'm not trying to rotate the object around the camera. I'm trying to rotate the object in place around the x,y, or z axis individual but with the reference of the camera's orientation (sorry for being redundant, I'm just trying to specify my problem in more detail).
Does anyone know how to do this? I tried to use the code below, but it did not work...
//get direction vector of camera to object and treat it as global axis of rotation
Vector3 dirVect = this.transform.position - camera.transform.position;
dirVect = Vector3.Scale(dirVect, new Vector3(1, 0, 1));
dirVect = dirVect.normalized; //dirVect now becomes direction of camera x axis?
I'd appreciate any help! Thank you for your time!!!
I don't have an exact answer for you, but I find it useful to temporarily make the object a child of the camera so you can use Unity's built-in relative rotation functions, then immediately un-child the object. Such temporary transform flips aren't a big drag on performance.
Answer by JVene · Jul 20, 2018 at 09:39 PM
Referencing Unity's Quaternion Documentation try something like:
Quaternion q = Quaternion.Euler( degxaxis, degyaxis, degzaxis );
q = camera.transform.rotation * q;
transform.rotation = transform.rotation * q;
My 3rd coffee hasn't been served, so think upon these lines as I'm not checking anything here. The idea is to fashion a Quaternion for the rotation on whatever axis you intend, then transforming that rotation in the camera's orientation, then applying the result to the object's rotation. I've not taken into account any selection of localRotation vs rotation, or any implication about mirroring, I haven't considered the order of multiplication (reversing the order produces different results), and I'm assuming the camera is looking directly at the object in question.
Your answer
Follow this Question
Related Questions
Rotating On Two Axes Independantly 0 Answers
Strange rotation pattern. 0 Answers
Rotate cube non-relative to current rotation. 1 Answer
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