U2L3——坐标系
本章代码关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   |  this.transform.position;             this.transform.rotation;             this.transform.eulerAngles;             this.transform.lossyScale;            
  this.transform.localPosition;             this.transform.localEulerAngles;         this.transform.localRotation;             this.transform.localScale;            
  Input.mousePosition;                 Screen.width                     Screen.height                    
 
  this.transform.InverseTransformPoint()         this.transform.InverseTransformDirection()     this.transform.InverseTransformVector()        
  this.transform.TransformPoint()             this.transform.TransformDirection()         this.transform.TransformVector()        
  Camera.main.WorldToScreenPoint()         Camera.main.ScreenToWorldPoint()         Camera.main.WorldToViewportPoint()         Camera.main.ViewportToWorldPoint()         Camera.main.ViewportToWorldPoint()         Camera.main.ScreenToWorldPoint()        
 
  | 
 
Unity坐标系
unity有各种不同的坐标系,这些坐标系相互之间也有转换方法
Unity各种坐标系
世界坐标系
原点是世界的中心点
世界坐标系的三个轴向是固定的

物体坐标系
原点是物体的中心点(建模时决定),轴向:
- 物体右方为X轴正方向
 
- 物体上方为Y轴正方向
 
- 物体前方为Z轴正方向
 
屏幕坐标系
原点:屏幕左下角
轴向:向右为 X 轴正方向,向左为 Y 轴正方向
最大宽高:Screen.width、Screen.height

视口坐标系
原点:屏幕左下角
轴向:向右为X轴正方向,向左为Y轴正方向
特点:左下角为(0, 0),右上角为(1, 1),和屏幕坐标类似,将坐标单位化
坐标获取
这里具体的内容在Transform,Input.mousePosition,Screen,Camera里有
直接获取
获取对象在物体世界坐标系的信息(Transform)
1 2 3 4
   | this.transform.position;             this.transform.rotation;             this.transform.eulerAngles;             this.transform.lossyScale;            
   | 
 
获取对象在父对象物体坐标系的信息(Transform)
1 2 3 4
   | this.transform.localPosition;             this.transform.localEulerAngles;         this.transform.localRotation;             this.transform.localScale;                
   | 
 
获取屏幕坐标系上的信息(Input.mousePosition,Screen.width)
1 2 3
   | Input.mousePosition;             Screen.width                     Screen.height                    
   | 
 
通过坐标转换获取
世界坐标系和父对象物体坐标系的转换(Transform的坐标转换)
1 2 3 4 5 6 7 8
   |  this.transform.InverseTransformPoint()         this.transform.InverseTransformDirection()     this.transform.InverseTransformVector()        
  this.transform.TransformPoint()                 this.transform.TransformDirection()             this.transform.TransformVector()            
 
  | 
 
世界坐标系和屏幕坐标系的转换Camera.WorldToScreenPoint()、Camera.ScreenToWorldPoint()
1 2
   | Camera.main.WorldToScreenPoint()         Camera.main.ScreenToWorldPoint()        
   | 
 
Camera的视口坐标系转换相关
视口坐标系与其他Unity坐标系相互转换的代码
1 2 3 4
   | Camera.main.WorldToViewportPoint()         Camera.main.ViewportToWorldPoint()         Camera.main.ViewportToWorldPoint()         Camera.main.ScreenToWorldPoint()        
   |