Transform有关旋转个属性和方法测试
一,属性
1,var eulerAngles : Vector3
- public float yRotation = 5.0F;
- void Update() {
- yRotation += Input.GetAxis("Horizontal");
- transform.eulerAngles = new Vector3(10, yRotation, 0);
- }
2,var rotation : Quaternion
- public float smooth = 2.0F;
- public float tiltAngle = 30.0F;
- void Update() {
- float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
- float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
- Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
- transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
- }
二,方法
1,function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) :void
- void Update() {
- transform.Rotate(Vector3.right * Time.deltaTime);
- transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
- }
效果,使物体旋转一个基于欧拉角的旋转角度,eulerAngles.z度围绕z轴,eulerAngles.x度围绕x轴,eulerAngles.y度围绕y轴。vector3可以变成3个分开的float值。
2,function Rotate (axis : Vector3, angle : float, relativeTo : Space = Space.Self) : void
- void Update() {
- transform.Rotate(Vector3.right, Time.deltaTime);
- transform.Rotate(Vector3.up, Time.deltaTime, Space.World);
- }
效果:按照angle度围绕axis轴旋转。
3,function RotateAround (point : Vector3, axis : Vector3, angle : float) : void
- public Transform target;
- Quaternion rotation;
- void Update()
- {
- transform.RotateAround(target.position, Vector3.up, 20 * Time.deltaTime);
- }
4,function LookAt (target : Transform, worldUp :Vector3 = Vector3.up) : void
- public Transform target;
- void Update() {
- transform.LookAt(target);
- }