一 坐標系簡介
1 世界坐標系
可以用transform.position獲取

世界坐標系
2 局部坐標系
局部坐標系是相對于父物體來說的:
當2個游戲對象互為父子關系,那么子物體就會以父物體的坐標點為自身坐標原點

局部坐標系
下面是子物體的Transform組件和打印的坐標結果,可以看出Transform組件顯示的坐標是相對于父物體為坐標原點的

子物體的Transform組件

打印結果
3 相機坐標系
根據觀察位置和方向建立坐標系。
用次坐標系可以方便判斷物體是否在相機前面,以及物體之間先后遮擋順序,它會優(yōu)先渲染離它最近的物體
4 屏幕坐標系
建立在屏幕上的二維坐標系,用來描述像素在屏幕上的位置
以屏幕左下角為坐標原點(0,0)。
右上角為(Screen.width,Screen.height),
void Update () {
if (Input.GetMouseButtonDown(0))
{
//獲取游戲界面鼠標左鍵點擊的坐標
Debug.Log(Input.mousePosition);
}
}
void Start (){ //獲取游戲界面里選中物體的坐標
Vector3 vc = Camera.main.WorldToScreenPoint(transform.position);
Debug.Log(vc);
}
5 視口坐標系
視口坐標是標準的和相對于相機的。相機左下角為(0,0)點,右上角為(1,1)點。
二 坐標系的轉換
1 全局坐標系轉局部坐標系
public GameObject a;
void Start () {
Debug.Log("this的坐標" + this.transform.position);
Debug.Log("a的坐標" + a.transform.position);
//輸出的是a-this的坐標
Vector3 v2 = this.transform.InverseTransformPoint(a.transform.position);
Debug.Log("全局坐標變局部坐標" + v2);
}

打印結果
2 局部坐標系轉全局坐標系
public GameObject a;
void Start () {
Debug.Log("this的坐標" + this.transform.position);
Debug.Log("a的坐標" + a.transform.position);
//輸出的是a+this的坐標
Vector3 v1 = this.transform.TransformPoint(a.transform.position);
Debug.Log("局部轉全局" + v1);
}

打印結果