物体自旋转脚本

2021-10-11  本文已影响0人  玄策丶
using UnityEngine;
using System.Collections;
public class AllRotateControl : MonoBehaviour 
{
    public enum Axes
    {
        X_Axes,
        Y_Axes,
        Z_Axes
    }
    public float Swing = 1.0f;
    float radian = 0; // 弧度  
    public float perRadian = 0.03f; // 每次变化的弧度  
    float radius = 0.8f; // 半径  
    Vector3 oldPos; // 开始时候的坐标 
    public bool IsSwing = false; 
    public bool IsRotate = true;
    public float RotateSpeed;
    public Axes XYZ;
    // Use this for initialization
    void OnEnable () {
        oldPos = transform.position; // 将最初的位置保存到oldPos  
        radian = 0;
    }
    
    // Update is called once per frame 
    void Update () {
        if(IsRotate)
        {
            switch (XYZ) {
            case Axes.X_Axes:
                this.gameObject.transform.Rotate (RotateSpeed,0,0);
                break;
            case Axes.Y_Axes:
                this.gameObject.transform.Rotate (0,RotateSpeed,0);
                break;
            case Axes.Z_Axes:
                this.gameObject.transform.Rotate (0,0,RotateSpeed);
                break;
            default:
                break;
            }
        }

        if(IsSwing)
        {
            radian += perRadian; // 弧度每次加0.03  
            float dy = Mathf.Sin(radian) * radius * Swing; // dy定义的是针对y轴的变量,也可以使用sin,找到一个适合的值就可以  
            this.transform.position = oldPos + new Vector3 (0, dy, 0);  
        }
    }
}
 
上一篇下一篇

猜你喜欢

热点阅读