标签:mono sea float summary call public class 抖动 旋转
项目需要做一个茶壶的壶盖抖动的动画
RotationShake
=========================================================
using UnityEngine;
/// <summary>
/// 旋转抖动动画
/// </summary>
public class RotationShake : MonoBehaviour {
public float zAngle = 30f; // 左右摆动的角度
public float perAngle = 10f; // 每次更新增加的角度
public GameObject obj;
private bool isPositive = true; //是否正向增加
private float baseAngle;
private float positiveAngle;
private float negativeAngle;
public float currentZAngle;
// Use this for initialization
void Start () {
// localEulerAngles的角度是从0~360,直接相减为负数会转化
baseAngle = obj.transform.localEulerAngles.z;
positiveAngle = baseAngle + zAngle;
positiveAngle = positiveAngle > 180 ? positiveAngle - 360 : positiveAngle;
negativeAngle = baseAngle - zAngle;
negativeAngle = negativeAngle < -180 ? negativeAngle + 360 : negativeAngle;
}
// Update is called once per frame
void Update () {
currentZAngle = obj.transform.localEulerAngles.z > 180 ? obj.transform.localEulerAngles.z - 360f : obj.transform.localEulerAngles.z;
if (currentZAngle > positiveAngle)
{
isPositive = false;
}
else if (currentZAngle < negativeAngle)
{
isPositive = true;
}
if (isPositive)
{
obj.transform.localEulerAngles = new Vector3(obj.transform.localEulerAngles.x, obj.transform.localEulerAngles.y, obj.transform.localEulerAngles.z+ perAngle);
}
else
{
obj.transform.localEulerAngles = new Vector3(obj.transform.localEulerAngles.x, obj.transform.localEulerAngles.y, obj.transform.localEulerAngles.z - perAngle);
}
}
}
标签:mono sea float summary call public class 抖动 旋转
原文地址:http://www.cnblogs.com/alps/p/7823494.html