物体transform操作
一、物体数据获取
void Start()
{
Debug.Log(transform.position);
Debug.Log(transform.localPosition);
Debug.Log(transform.rotation);
Debug.Log(transform.localRotation);
Debug.Log(transform.eulerAngles);
Debug.Log(transform.localEulerAngles);
Debug.Log(transform.localScale);
Debug.Log(transform.forward);
Debug.Log(transform.right);
Debug.Log(transform.forward);
}
二、物体数据操作
1.看向原点
void Update()
{
transform.LookAt(Vector3.zero);
}
2.沿某一轴自转
void Update()
{
transform.Rotate(Vector3.up, 1);
}
3.围绕某个目标点旋转
void Update()
{
transform.RotateAround(Vector3.zero,Vector3.up,2);
}
4.移动
void Update()
{
transform.Translate(Vector3.forward*0.1f);
}
三、物体父子关系
void Start()
{
GameObject parent = transform.parent.gameObject;
Debug.Log(parent.name);
Debug.Log(transform.parent.gameObject.name);
Debug.Log(transform.childCount);
Transform trans = transform.Find("Sphere");
trans = transform.GetChild(0);
bool res = trans.IsChildOf(transform);
Debug.Log(res);
transform.DetachChildren();
res = trans.IsChildOf(transform);
Debug.Log(res);
trans.SetParent(transform);
res = trans.IsChildOf(transform);
Debug.Log(res);
}