私は、回転することを避けるために、同じ量のZ軸に沿って後方に(sprite
は円形ではありません)lerp
にします。これを行うには、私はランダムにforward angle
を計画し、それにはlerp
を保存し、同量のlerp
を戻します。しかし、これは、逆回転が始まると始まりの角度が同じではないようにいくつかの奇妙なポップを与える。私がそれを呼び出すとき、私はそれを同じ時間補間する。いくつかのコード:四元とクォータニオン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