我想沿着Z軸前後lerp
前後數量相同以避免旋轉(因爲我的sprite
不是圓形的)。要做到這一點,我計劃隨機使用forward angle
,存儲它,lerp
然後lerp
返回相同的金額。然而,這會給出一些奇怪的爆裂,因爲當向後旋轉開始時,開始角度不會是相同的,但是它是。當我打電話給它時,我同時插入它。有些代碼:Quaternion lerp來回
IEnumerator LerpQuat(QuatLerpInput rotThis, float time, float leftBoundary, float rightBoundary)
{
/*
* we want to rotate forward, then backward the same amount
* to avoid spinning. we store the given value to both of these.
*/
Transform stuffToRot = rotThis.godRayGO.transform;
float lastTime = Time.realtimeSinceStartup;
float timer = 0.0f;
switch (rotThis.rotState)
{
case (QuatLerpInput.RotationStates.rotAway):
rotThis.deltaRot = Random.Range(leftBoundary, rightBoundary);
while (timer < time)
{
stuffToRot.rotation = Quaternion.Euler(stuffToRot.rotation.x, stuffToRot.rotation.y,
Mathf.LerpAngle(rotThis.idleRot, rotThis.idleRot + rotThis.deltaRot, timer/time));
timer += (Time.realtimeSinceStartup - lastTime);
lastTime = Time.realtimeSinceStartup;
yield return null;
}
rotThis.rotState = QuatLerpInput.RotationStates.setBack;
break;
case (QuatLerpInput.RotationStates.setBack):
while (timer < time)
{
stuffToRot.rotation = Quaternion.Euler(stuffToRot.rotation.x, stuffToRot.rotation.y,
Mathf.LerpAngle(rotThis.idleRot + rotThis.deltaRot, rotThis.idleRot, timer/time));
timer += (Time.realtimeSinceStartup - lastTime);
lastTime = Time.realtimeSinceStartup;
yield return null;
}
rotThis.rotState = QuatLerpInput.RotationStates.rotAway;
break;
}
}
public class QuatLerpInput
{
public GameObject godRayGO;
public float deltaRot;
public float idleRot;
public enum RotationStates
{
rotAway, setBack
}
public RotationStates rotState = RotationStates.rotAway;
public QuatLerpInput(GameObject godRayGO)
{
this.godRayGO = godRayGO;
deltaRot = godRayGO.transform.rotation.z;
idleRot = godRayGO.transform.rotation.z;
}
}
編輯switch
與四元數:
switch (rotThis.rotState)
{
case (QuatLerpInput.RotationStates.rotAway):
rotThis.deltaRot = Random.Range(leftBoundary, rightBoundary);
Quaternion destination = new Quaternion(rotThis.idleQuat.x, rotThis.idleQuat.y, rotThis.idleQuat.z + rotThis.deltaRot, 1.0f);
rotThis.deltaQuat = destination;
while (timer < time)
{
stuffToRot.rotation = Quaternion.Slerp(rotThis.idleQuat, rotThis.deltaQuat, timer/time);
//stuffToRot.rotation = Quaternion.Euler(stuffToRot.rotation.x, stuffToRot.rotation.y,
// Mathf.LerpAngle(rotThis.idleRot, rotThis.idleRot + rotThis.deltaRot, timer/time));
timer += (Time.realtimeSinceStartup - lastTime);
lastTime = Time.realtimeSinceStartup;
yield return null;
}
rotThis.rotState = QuatLerpInput.RotationStates.setBack;
break;
case (QuatLerpInput.RotationStates.setBack):
while (timer < time)
{
stuffToRot.rotation = Quaternion.Slerp(rotThis.deltaQuat, rotThis.idleQuat, timer/time);
//stuffToRot.rotation = Quaternion.Euler(stuffToRot.rotation.x, stuffToRot.rotation.y,
// Mathf.LerpAngle(rotThis.idleRot + rotThis.deltaRot, rotThis.idleRot, timer/time));
timer += (Time.realtimeSinceStartup - lastTime);
lastTime = Time.realtimeSinceStartup;
yield return null;
}
rotThis.rotState = QuatLerpInput.RotationStates.rotAway;
break;
}
如果有人偶然發現並無法幫助,請隨時要求澄清。我竭盡全力,但我可能已經表達了自己的不清楚,並且在我的最後仍然沒有找到答案。 – agiro