一、物體的旋轉(zhuǎn)
給物體調(diào)轉(zhuǎn)一個(gè)旋轉(zhuǎn)角度。
1、Quaternion四元組(x,y,z,w)
transfrom.rotation()=... 不方便操作,官方不建議用
2、Euler Angle 歐拉角
transfrom.eulerAngles = new Vector(0,45,0);
transfrom.LocalE ulerAngles = new Vector(0,45,0);
void Start(){
transfrom.localEulerAngles = new Vector(0,45,0);
}
這樣就可以讓物體旋轉(zhuǎn)45度。
在Update 中修改角度持續(xù)旋轉(zhuǎn)
Vector3 angles = transfrom.localEulerAngles;
anlges.y +=0.5f;
transfrom.localEulerAngles = angles;
優(yōu)化,使之勻速旋轉(zhuǎn)
float rotateSpeed = 30;
void Update(){
Vector3 angle = transfrom.localEulerAngles;
angle.y +=0.5f;
transfrom.localEulerAngles = angle;
}
勻速旋轉(zhuǎn):
void Update(){
float rotateSpeed = 30;
Vector3 angle = transfrom.localEulerAngles;
angle.y +=rotateSpeed * Time.deltaTime;
transfrom.localEulerAngles = angle;
}
二、相對旋轉(zhuǎn)
Rotate():旋轉(zhuǎn)一個(gè)相對角度
transfrom.Rotate(dx,dy,dz,space)
void Update(){
float rotateSpeed = 30;
float speed = rotateSpeed * Time.deltaTime;
this.transfrom.Rotate(0,speed,0,Space.Self);
}
三、自轉(zhuǎn)與公轉(zhuǎn)
自轉(zhuǎn):繞著自身軸旋轉(zhuǎn)
公轉(zhuǎn):圍繞另一個(gè)物體旋轉(zhuǎn)
當(dāng)父物體旋轉(zhuǎn)時(shí),帶動(dòng)子物體一并旋轉(zhuǎn)。
例:衛(wèi)星圍繞地球轉(zhuǎn)
viod Update(){
float rotateSpeed = 60;
float speed = rotateSpeed * Time.deltaTime;
Transfrom parent = this.transfrom.parent;
parent.Rotate(0,speed,0,Space.Self);
}
找到需要進(jìn)行公轉(zhuǎn)的物體的父物體,讓父物體旋轉(zhuǎn)。
使地球繞著地球的y軸旋轉(zhuǎn),使地球初始位置設(shè)為0(和地月系統(tǒng)的位置相同),而衛(wèi)星繞著a1這個(gè)物體做公轉(zhuǎn),使a1旋轉(zhuǎn)可以帶動(dòng)衛(wèi)星旋轉(zhuǎn)。由于地球和衛(wèi)星所繞的旋轉(zhuǎn)點(diǎn)不同,所以其角速度可以分別調(diào)節(jié)。
四、官方文檔
unity.cn
-手冊 manual
-Script API
Unity User Manual 2021.3 (LTS) - Unity 手冊
unity 在安裝的時(shí)候也有一份英文文檔文章來源:http://www.zghlxwxcb.cn/news/detail-415288.html
地址:Uhub\2021.3.18f1c1\Editor\Data\Documentation\en文章來源地址http://www.zghlxwxcb.cn/news/detail-415288.html
到了這里,關(guān)于Unity3D 腳本3(旋轉(zhuǎn))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!