一、位移
一般选择第三种方法实现位移
1、位移计算公式
一维坐标系:路程=速度*时间;
多维坐标系:路程=速度*方向*时间;
2.直接计算的方法
position=当前位置+移动距离
this.transform.position=this.transform.position+this.transform.up*1*Time.deltaTime;
方向非常重要,因为它决定了前进方向
this.transform.position+=Vector3.forward*1*Time.deltaTime;
3.API计算方法
参数一:位移的多少。
参数二:相对的是什么坐标系
相对世界坐标系Z轴动
this.transform.Translate(Vector3.forward*1*Time.deltaTime,Space.World);
相对世界坐标的,自己的面朝向去动,始终朝自己的面朝向移动
this.transform.Translate(this.transform.forward*1*Time.deltaTime,Space.World);
相对于自己的坐标系下,自己面朝向量移动(一定不会让物体这样移动)
this.transform.Translate(this.transform.forward*1*Time.deltaTime,Space.Self);
相对于自己的坐标系下Z轴正方向移动,始终朝自己的面朝向移动
this.transform.Translate(Vector3.forward*1*Time.deltaTime,Space.Self);
二、角度
在Start函数中
相对世界坐标的角度
print(this.transform.eulerAngles);
相对父对象的角度
print(this.transform.localEulerAngles);
设置角度和位移一样,不能单一设置,只能一起改变/如果是改变面板上面显示的角度,就改变相对父对象的角度
三、旋转
在Update函数中
this.transform.Translate (this.transform.forward*1*Time. deltaTime, Space. World);
this.transform. position += Vector3.forward * Time. deltaTime;
API计算旋转相关:
1、自转
每个轴具体转多少度
参数一:每一帧旋转的角度
参数二:相对坐标系,如果默认不填就是相对自己的坐标系
this.transform. Rotate (new Vector3(0,10,0)*Time. deltaTime);
this.transform.Rotate(new Vector3(0,10,0) *5* Time. deltaTime,Space.World);
2、相对于某个轴转动了多少度
参数一:相对于哪个轴转动
参数二:转动的角度是多少
参数三:默认不写就是相对于自己的坐标系,如果填写,可以填相对于世界坐标系进行旋转
this.transform.Rotate(Vector3.right,5 * Time. deltaTime);
this.transform.Rotate(Vector3.right,5 * Time.deltaTime,Space. World);
3、相对于一个点旋转
参数一:相对于哪一个点
参数二:相对于点的哪个轴
参数三:转动的速度:旋转速度*时间
this.transform.RotateAround(new Vector3(0,1,0), Vector3.right,10*Time. deltaTime);
四、缩放
同样 缩放也不能单独该x、y、z,只能一起改
相对于世界坐标系的缩放大小 只能得 不能改,所以如果要用代码修改缩放大小,都是改相对父对象得缩放大小
Unity没有提供关于缩放的API,之前的位移、旋转都提供了对应的API,但缩放没有
1、获取相对世界坐标系的缩放数值
print(this.transform.lossyScale);
2、获取相对本地坐标系的缩放数值(相对父对象)
print(this.transform.localScale);
3、直接修改范例
this.transform.localScale = new Vector3(3, 3, 3);
4、慢慢变大范例(需写在Update里)
this.transform.localScale += Vector3.one * Time.deltaTime;
五、看向
看向:让一个对象的面朝向,一致看向某一个点 或者某一个对象
1、看向一个点就传入一个点
this.transform.LookAt(Vector3.zero);
2、看向一个对象就传入一个对象的Transform
this.transform.LookAt(lookAtObj);