Get object orientation

Hi there!

I’m here to ask if it’s possible to store an object (camera) orientation into a variable, so I can later use it to “match” another object (another camera) with it?

The objective is to make a “smooth transition” when I advance to the next “slide” in the scene (I’m creating a presentation based on Prezi “Zoom In Presentation”, but in 3D), knowing that the second set of objects is in a different direction than the first set…

Thank in advance!

JPB18

I suggest you create an empty gameobject for each positions - orientation you will have. Set them up properly in the editor, declare an array of transform in your script and then you can access the information you need at any time.

If you still want to only store a rotation though, declare a quaternion and affect you’re camera’s rotation to it.

Yepp. A camera (like any other gameobject in unity) has a transform property that stores position, orientation and scaling of the object. If you are only interested in the orientation, store transform.rotation (type Quaternion). You can also smoothly interpolate between the two orientations by using Quaternion.Slerp()

Further info:
http://unity3d.com/support/documentation/ScriptReference/Transform.html

The best approach would be to create an empty GameObject for every pos/rot/scale setting you want to store. The additional advantage is, you could create/place them beforehand in your scene, and modify them with the transform handles, or with Ctrl-Shift-F and “AlignViewToSelected” etc.

As there are no Copy methods for the whole transform, you’d have to store/load the values individually. For example, to store the values, you’d do something like:

Transform mainCamTrans=Camera.main.transform;
myTransfomStorage[x].position=mainCamTrans.position;
myTransfomStorage[x].rotation=mainCamTrans.rotation;
myTransfomStorage[x].localScale=mainCamTrans.localScale; // there is no reliable global scale, but it's irrelevant for Cameras anyway, only for objects

Note you’d need special treatment (i.e., a small script attached to that empty GameObject) for additional camera settings such as FoV, near/far clip, …