Unity, camera space and camera gameobject local space

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.