【Unity】Transform的位移、角度、旋转、缩放以及看向

一、位移

一般选择第三种方法实现位移

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hiOoo.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值