目录
transform组件
属性
ps.属性可以大概看成变量
forward
属性 | forward(类似right、up) //初学可以把属性看成变量 |
语法 | public Vector3 forward{set;get;}; |
用法 | 返回或设置transform自身坐标系中z轴方向的单位向量对应的世界坐标系中的单位向量 |
localPosition(localRotation)
属性 | localPosition(localRotation) |
语法 | public Vector3 localPosition{get;set;}; |
用法 | 此属性用于返回该物体的在局部坐标系中的位置。 |
childCount
属性 | childCount |
语法 | public int childCount{get;}; |
用途 | 用于返回对象的子物体的数量。 |
root
属性 | root |
语法 | public Transform root{get;}; |
用途 | 此属性用于返回对象的最高级父物体。 |
localScale
属性 | localScale(面板上那个Scale) |
语法 | public Vector3 localScale{get;set;}; |
用途 | 相对于父级的变换。设置和更改物体的三个轴向的缩放 |
lossyScale(返回物体相对于世界坐标系的缩放值)
parent
属性 | parent |
语法 | public Transform parent{set;get;}; |
用法 | 用于设置或返回transform对象的父物体。如果想让一个子物体变成无父级物体用transform.parent=null; |
//属性,左手坐标系
forword同right left up
前z,右x,上y
//V3类型的属性
语法 public Vector3 forword{set;get;};
返回或设置transform自身坐标系中z轴方向的单位向量对应的世界坐标系中的单位向量
方法
获取鼠标按钮
//获取鼠标按钮
Input.GetMouseButton()//按住
Input.GetMouseButtonDown()//按下
0//鼠标左键
1//鼠标右键
2//中间滚轮
LooAt()
方法(函数) | LooAt() // transform组件 |
用法 | public void LookAt (Transform target) public void LookAt(Vector3 WorldPosition) public void LookAt(Transform target,Vector3 WorldUp) public void LookAt(Vector3 WorldPosition,Vector3 WorldUp) |
用途 | 使摄像机看向实例 |
eg. | 摄像机.transform.LookAt(实例.transform.position); |
eg. | //让摄像机看向实例上方n米处 |
方法 | LookAt() |
用法 | public void LookAt(Transform target) public void LookAt(Vector3 WorldPosition) public void LookAt(Transform target,Vector3 WorldUp) public void LookAt(Vector3 WorldPosition,Vector3 WorldUp) |
用途 | 将参数target或worldPosition作为当前transform的z轴指向方向。y轴的指向方向为 WorldUp向量在 实例对象旋转后x,y平面的投影方向。 |
1、动态创建一个球体,圆柱,立方体,要求立方体作为圆柱的子物体,且在圆柱前方向1米的位置,然后控制圆柱体看向球体,球体坐标随机
RotateAround()
方法(函数) | RotateAround() //transform组件 |
用法 | ublic void RotateAround(Vector3 point,Vector3 axis,float angleSpeed) |
用途 | 绕某个point点的axis方向旋转 |
解释 | 参数axis为要旋转的轴方向,angleSpeed是旋转角度 |
Rotate&Rotation
方法(函数) | Rotate&Rotation // transform组件 |
语法 | public void Rotate(Vector3 axisAngleSpeed) public void Rotate(Vector3 axisAngleSpeed,Space relativeTo) public void Rotate(float x,float y,float z) public void Rotate(float x,float y,float z,Space relativeTo) 参数eulerAngles为要旋转的欧拉角,relativeTo为参考坐标系 |
区别 | Rotate()方法是:旋转多少度。是动作,参数为速度,是角度值。 rotation属性是:当前四元数值。 |
给欧拉角赋值
transform.rotation = Quaternion.Euler(30, 30, 30);
transform.eulerAngles = new Vector3(30, 30, 30);
1.欧拉角的分量不能单独赋值,要给一起给;但是可以单独获取
2.欧拉角,x的返回值范围是0~90和270~360,y,z的返回量返回范围是0~360
3.enlerAngles的返回角度是世界坐标(考虑父级转动的度数),-localEnlerAngles是面板上的数据
(只考虑自身的度数)
eg.调整摄像机视角
void Update()
{
//让摄像机看向物体
transform.LookAt(tank.transform.position + tank.transform.up);
//绕着物体转一圈
//transform.RotateAround(tank.transform.position, Vector3.up, 0.1f);
//点击左键
if (Input.GetMouseButton(0))
{
//绕着物体转一圈
transform.RotateAround(tank.transform.position, Vector3.up, 1f);
}
else
//线性差值运动
transform.position =
Vector3.Lerp(transform.position, tank.transform.position + Vector3.up * 4 - tank.transform.forward * 10, 0.01f);
}
eg.模拟太阳月亮地球转圈(不严谨)
void Update()
{
// Time.deltaTime
//月亮绕地球转
moon.transform.RotateAround(earth.transform.position, Vector3.up, 1f);
//地球绕太阳转
earth.transform.RotateAround(sun.transform.position, Vector3.up, 1f);
//自转
moon.transform.Rotate(Vector3.up * 1f);
earth.transform.Rotate(Vector3.up * 1f);
sun.transform.Rotate(Vector3.up * 1f);
//需要把月亮设置为地球的子物体,要不然月亮会往外转
}
Translate()
方法(函数) | Translate() |
用途 | 一直朝某个方向移动 |
用法(四种重载) | public void Translate(Vector3 translation) //无返回值 public void Translate(Vector3 translation,Space relativeTo) public void Translate(float x,float y,float z) public void Translate(float x,float y,float z,Space relativeTo) |
解释 | relativeTo参考坐标系 translation移动向量 返回值是确切的坐标,确切的结果,这里是一直走,所以没有固定的坐标,没有返回值 |
eg. | //前进一米 transform.Translate(Vector3.forward); //减速 transform.Translate(Vector3.forward*0.01f); |
eg. | //按K往前走 if(Input.GetKey(KeyCode.W)) { cb.transform.Translate(Vector3.forward * 0.1f); } |
人物的简单移动
//人物的简单移动(键盘控制,有旋转)
public GameObject player;
void Update()
{
if (Input.GetKey(KeyCode.I))
{
player.transform.Translate(Vector3.forward * 0.1f);
}
if (Input.GetKey(KeyCode.K))
{
player.transform.Translate(-Vector3.forward * 0.1f);
}
if (Input.GetKey(KeyCode.J))
{
player.transform.Rotate(-Vector3.up * 0.1f);
}
if (Input.GetKey(KeyCode.L))
{
player.transform.Rotate(Vector3.up * 0.1f);
}
eg.
//人物的简单移动(键盘控制,无旋转)
if(Input.GetKey(KeyCode.W))
{
cb.transform.Translate(Vector3.forward * 0.1f);
}
if (Input.GetKey(KeyCode.S))
{
cb.transform.Translate(Vector3.back * 0.1f);
}
if (Input.GetKey(KeyCode.A))
{
cb.transform.Translate(Vector3.left* 0.1f);
}
if (Input.GetKey(KeyCode.D))
{
cb.transform.Translate(Vector3.right * 0.1f);
}
DetachChildren()
方法 | DetachChildren() |
语法 | public void DetachChildren() |
用法 | 用于将自身子物体分离。 |
无参数,直接调用
GetChild()
方法 | GetChild() |
用法 | public Transform GetChild(int index) |
用途 | 此方法用于返回Transform索引值为index的子物体Transform。(得到子物体) |
//获得子物体信息
if(Input.GetKeyDown(KeyCode.T))
{
Transform r = player.transform.GetChild(0);//返回值为第一个子物体的
print(r.name);//打印子物体名称
}
Find()
方法 | Find() |
用法 | public Transform Find(string name) |
用途 | 此方法根据名字查找物体的子物体Transform。 |
GameObject.Find()和transform.Find()的区别
1、返回类型不同
2、GameObject.Find()全局查找根场景下子物体,transform.Find()查找的是自身子物体,
3、GameObject.Find()无法找打ActiveSelf为false的物体
transform.Find()可以
IsChildOf()
方法 | IsChildOf() |
用法 | public bool IsChildOf(Transform parent) |
用途 | 用于判断当前实例Transform是否为parent的子物体 |
SetParent()
方法 | SetParent() 设置物体的父级 |
用法 | public void SetParent(Transform parent); public void SetParent(Transform parent, bool worldPositionStays); |
第一个是我们常用的只有一个参数的函数,第二个是加了一个是否保持世界坐标系的布尔型变量。
如果为真,那么就保持之前的位置、旋转量、缩放值。而为否,的时候则保持局部坐标的位置、旋转、缩放。即相对缩放值都保持1。
1、建立动态一个立方体和球体,立方体的缩放为2,2,2,点击球体把球体变成立方体的子物体,并不随着父级缩放,且位置在父物体右方1米的位置。
SetActive()
eg.使物体消失
//使物体消失
gameObject.SetActive(false);
//版本2
gameObject.SetActive(b);
b = !b;
TransformDirection()
方法 | TransformDirection() |
用法 | public Vector3 TransformDirection(Vector3 index) public Vector3 TransformDirection(float x,float y,float z) |
用途 | 此方法用于将局部坐标向量变成世界坐标系向量 |
SetPositionAndRotation()
方法 | SetPositionAndRotation() |
用法 | public void SetPositionAndRotation(Vector3 position, Quaternion rotation); |
用途 | 同时设置物体的坐标和角度 |