unity物体左右摇摆(旋转)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Swing : MonoBehaviour
{
    private bool turnRight;
    private bool turnLeft;
    private float turnDegree=0;
    void Start()
    {
        if (this.transform.localRotation.z==0)
        {
            turnRight = true;
            turnLeft = false;
        }
    }

    // Update is called once per frame
    void Update()
    {   
        Right();
        Left();
    }


    /// 
    /// 向右摇摆
    /// 
    private void Right()
    {
        if (turnRight == true)
        {
            if (turnDegree<2.0f)
            {
                turnDegree += Time.deltaTime;
                this.transform.localEulerAngles =new Vector3 (0,0,turnDegree);
            }
            else
            {
                turnRight = false;
                turnLeft = true;
            }
        }
    }

    private void Left()
    {
        if (turnLeft == true)
        {
            if (turnDegree>-2.0f)
            {                
                turnDegree -= Time.deltaTime;
                this.transform.localEulerAngles = new Vector3(0, 0, turnDegree);
            }
            else
            {
                turnRight = true;
                turnLeft = false;
            }
        }
    }
}

你可能感兴趣的:(unity,游戏引擎)