Understanding worldToCameraMatrix and cameraToWorldMatrix

Hell Everyone,

Can anyone please help understanding in following properties? This is regard to Camera object in Unity.

My question:

  • When do we use the properties "cameraToWorldMatrix" and "worldToCameraMatrix"?
  • Please provide me an example that would explain me necessity of these two properties.
  • What difficulty we would face if we don't use them.
Basically I'm not able to understand these properties related camera.
Thanks,

Sanjay Maurya

So, to understand these members of the Camera class, you need to be comfortable with 3d geometry and matrices. Both “world space” and “camera space” are 3d cartesian coordinate systems. All 3d points in one space can be transformed into the 3d space of the other using a matrix. Suppose for example you position the camera at the world point (0,0,-1) with the camera pointing in the positive world space z direction. Now, any point in camera space can be transformed into world space, by, you guessed it, adding one to the z component. So, the point that is one unit in front of the camera (0,0,1)cs is (0,0,0)ws. (Where I use cs and ws to denote the space.) Similarly, you can transform points in world space into camera space. The point(1,1,1)ws is (1,1,0) camera space. I’ve explained this using just changes in position. In the “real world” your camera will be rotated, and then the transforms from camera space to world space, and back, become more complicated.

Unity conveniently gives you these transform matrices. These are what Camera.cameraToWorldMatrix and Camera.worldToCameraMatrix give you. You only need to use these if you explicitly want to know how to get some one space to the other. I suspect 99% of people using Unity never need to do this. If you don’t use these properties nothing bad will happen.