transform.position和transform.localPosition區別

1. position是根據世界原點爲中心


2. localPosition是根據父節點爲中心,若是沒有父節點,localpositon和position是沒有區別的


3.選中一個物體左上角Global和Local切換看物體世界座標軸和本地座標軸this

 

using UnityEngine;
using System.Collections;


public class TestPostiton : MonoBehaviour {
void OnGUI() 
{
if (GUI.Button(new Rect(0, 0, 200, 50), "世界座標移動"))
{
this.gameObject.transform.position = new Vector3(gameObject.transform.position.x+1
,gameObject.transform.position.y
, gameObject.transform.position.z);
}


if (GUI.Button(new Rect(0, 50, 200, 50), "本地座標移動"))
{
this.gameObject.transform.localPosition = new Vector3(gameObject.transform.localPosition.x+1
, gameObject.transform.localPosition.y 
, gameObject.transform.localPosition.z);
}


GUI.Label(new Rect(210, 0, 300, 50), "世界座標:" + string.Format("({0},{1},{2})", gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z));
GUI.Label(new Rect(210, 50, 300, 50), "本地座標:" + string.Format("({0},{1},{2})", gameObject.transform.localPosition.x, gameObject.transform.localPosition.y, gameObject.transform.localPosition.z));




}


}
相關文章
相關標籤/搜索