组件介绍
Transform组件在每个游戏对象中都存在,且只存在一个。该组件保存了游戏对象的位置、平移、旋转、缩放等信息。
组件相关方法
//获取当前游戏对象的Transform组件
this.transform;
getObject.transform;
GetComponent<Transform>();
//属性
gameObject//游戏对象
name、tag//名字和标签
Position、LocalPosition//位置
eulerAngles、LocalEulerAngles//欧拉角
rotation、LocalRotation//旋转(四元数)
scale、LocalScale//缩放
right、up、forward//游戏对象的右方、上方、前方
root、parent、hierarchyCount、childCount// 层级相关
camera、light// 相机和灯光
animation、audio、collider、renderer、rigidbody// 游戏对象的其他组件
//方法
// 平移
Translate(Vector3 translation)
// 绕eulerAngles轴自转,自转角度等于eulerAngles的模长
Rotate(Vector3 eulerAngles)
// 绕过point点的axis方向的轴公转angle度
RotateAround(Vector3 point, Vector3 axis, float angle)
// 绕过point点的axis方向的轴公转angle度(坐标系采用本地坐标系)
RotateAroundLocal(Vector3 point, Vector3 axis, float angle)
// 看向transform组件的位置
LookAt(transform)
// 获取组件类型
GetType()
// 获取子游戏对象的Transform组件(不能获取孙子组件)
FindChild("name")
// 获取子游戏对象的Transform组件(可以获取孙子组件)
Find("name")
// 通过索引获取子游戏对象的Transform组件(不能获取孙子组件)
GetChild(index)
// 获取游戏对象的子对象个数
GetChildCount()
// 获取其他组件
GetComponent<T>
// 在子游戏对象中获取组件
GetComponentsInChildren<T>()