旋转抖动
项目需要做一个茶壶的壶盖抖动的动画
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);
}
}
}
============================== 用Quaternion实现 ===============================================
using UnityEngine;
/// <summary>
/// 旋转抖动动画
/// </summary>
public class LerpShake : MonoBehaviour {
public float zAngle = 30f;
public float yAngle = 30f;
private float rYAngle;
private float rZAngle;
public bool isZPositive = true; //z轴旋转是否正向增加
public bool isYPositive = true;
public float zRotateSpeed = 0.5f;
public float yRotateSpeed = 0.5f;
private Quaternion przq; // 随机z轴抖动角度的正向旋转
private Quaternion nrzq; // 随机z轴抖动角度的反向旋转
private Quaternion pryq; // 随机y轴旋转角度
private Quaternion nryq; // 随机y轴旋转角度
private Quaternion baseRotation;
private Quaternion baseYRotation;
public float zt;
public float yt;
private float timer =0f;
// Use this for initialization
void Start () {
baseRotation = transform.rotation;
baseYRotation = transform.rotation;
RandomZAngles();
RandomYAngles();
}
void RandomZAngles() {
rZAngle = Random.Range(0f,zAngle);
przq = baseRotation * Quaternion.Euler(0,0, rZAngle);
nrzq = baseRotation * Quaternion.Inverse(Quaternion.Euler(0, 0, rZAngle));
}
void RandomYAngles() {
baseYRotation = transform.rotation;
isYPositive = Random.value > 0.5f;
rYAngle = Random.Range(0, yAngle);
pryq = baseYRotation * Quaternion.Euler(0, rYAngle, 0);
nryq = baseYRotation * Quaternion.Inverse(Quaternion.Euler(0, rYAngle, 0));
}
// Update is called once per frame
void Update () {
zt += Time.deltaTime * zRotateSpeed;
yt = Time.deltaTime * yRotateSpeed;
if (isZPositive)
{
transform.rotation = Quaternion.Lerp(transform.rotation, przq, zt);
if (zt >= 1)
{
zt = 0;
isZPositive = false;
}
}
else {
transform.rotation = Quaternion.Lerp(transform.rotation, nrzq, zt);
if (zt >= 1)
{
zt = 0;
isZPositive = true;
RandomZAngles();
}
}
if (isYPositive)
{
transform.rotation = Quaternion.Lerp(transform.rotation, pryq, yt);
//transform.Rotate(0,yt,0,Space.World);
}
else {
transform.rotation = Quaternion.Lerp(transform.rotation, nryq, yt);
//transform.Rotate(0, -yt, 0, Space.World);
}
if (yt >= 1)
{
yt = 0;
RandomYAngles();
}
//timer += Time.deltaTime;
//if (timer >= 3f) {
// RandomYAngles();
//}
}
}
deltaTime的替换方案
var pointA:Vector3; var pointB:Vector3; var time:float = 10.0; private var i:float = 0.0; private var rate:float = 0.0;
function Update () {
MoveObject(this.transform, pointA, pointB, time);
}
function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) { rate = 1.0/time; if (i < 1.0) { i += Time.deltaTime * rate; thisTransform.position = Vector3.Lerp(startPos, endPos, i); } }