In unity, camera space does not coincide with the camera Gameobject's local space.
In the Camera.worldtocameramatrix document, there is this sentence:
Note that camera space matches OpenGL Convention:camera's forward is the negative Z axis. This was different from Unity's convention, where forward is the positive Z axis.
This means that the blue axis of the camera Gameobject in unity is the-Z of the camera space.
In order to confirm, the following experiments were done:
, the coordinates of the cube are (0,0,0), the coordinates of the camera are (0,0,-3), we want to calculate and output the coordinates of the cube in the camera space.
This is done by adding the following script to the cube:
using unityengine;
using system.collections;
public class printposincameraspace : monobehaviour {
public gameobject m_cameraref;
// use this for initialization
void Start () {
matrix4x4 worldtocameramat = m_cameraref.getcomponent<camera> (). Worldtocameramatrix;
vector3 thisposinworld = transform.position ;
Vector3 thisPosInCamera = worldtocameramat.multiplypoint (Thisposinworld);
Debug.Log (Thisposincamera);
}
}
and assign the camera to M_cameraref in the editor.
Then run the script and get the output as: (0,0,-3).
This means that the blue axis of the camera Gameobject is indeed the camera space-Z axis.
Further experiments:
Change the script to:
Using Unityengine;
Using System.Collections;
public class Printposincameragameobjectlocalspace:monobehaviour {
Public Gameobject M_cameraref;
Use this for initialization
void Start () {
matrix4x4 Worldtocameramat = M_cameraRef.transform.worldToLocalMatrix;
Vector3 Thisposinworld = transform.position;
Vector3 Thisposincamera = Worldtocameramat.multiplypoint (Thisposinworld);
Debug.Log (Thisposincamera);
}
}
The output is: (0,0,3).
--Conclusion:
camera space and camera gameobject of the local space is not coincident. the three axes in the figure represent the local space of the camera gameobject. The camera space is the opposite of the z-axis.
Camera.worldtocameramatrix is the world space to camera space matrix. Camera.transform.worldToLocalMatrix is the local space matrix of the world space to camera gameobject, and the two matrices are not the same.
Unity, camera space and camera gameobject local space