【Unity快速实现小功能】实现物体绕轴旋转

问题描述:实现一个物体能绕x,y,z某轴旋转的功能,并能控制其旋转速度及方向。

问题解决:

1) 将组件RotationOnTheAxis添加至物体中即可。

2) 组件RotationOnTheAxis代码如下:

</pre><pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class RotationOnTheAxis : MonoBehaviour {

    /**
     * 设置沿X / Y / Z轴旋转速度,
     * 正数为正向旋转,负数为负方向旋转,
     * 数值越大旋转速度越快,越小越慢
     */
    public float rotationSpeedX = 90;
    public float rotationSpeedY = 0;
    public float rotationSpeedZ = 0;

    void Update()
    {
        transform.Rotate(new Vector3(rotationSpeedX, rotationSpeedY, rotationSpeedZ) * Time.deltaTime);
    }
}


3) 按照自己的需求分别设置rotationSpeedX / rotationSpeedY / rotationSpeedZ即可。


猜你喜欢

转载自blog.csdn.net/Winner_2012/article/details/44422929