- Home /
few question about quaternion slerp
hi everyone.
i have a coroutine that looks like this
private IEnumerator StartRotation(
Quaternion curPos,
Quaternion targetPos,
float t
)
{
float counter = 0;
while ( counter < t)
{
counter += Time.deltaTime;
transform.rotation = Quaternion.Slerp(curPos,targetPos,counter);
yield return null;
}
}
well, it's working, but i need two more things and i can't figure out hot to do it...
i want to be able to pingpong gameobject between this two rotation
maybe is a noob question but if i want that slerp complete the rotation in 1 second, ho i should set it?
i'm confused.. someone can help me please?
thank's you in advance
Answer by Legend_Bacon · Nov 27, 2018 at 02:41 PM
Hello there,
I realize this question has been answered already, but I've already written and tested my answer, so here goes anyways:
private IEnumerator RotateCoroutine(Quaternion startRot, Quaternion endRot, float duration)
{
//Insert your own condition here.
// If you leave "true", it will run forever.
while (true)
{
float timer = 0.0f;
while (timer < duration)
{
timer += Time.deltaTime;
float t = timer / duration;
//Smoothing algorithm -- Take it out if you don't want it.
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.rotation = Quaternion.Slerp(startRot, endRot, t);
yield return null;
}
timer = 0.0f;
while (timer < duration)
{
timer += Time.deltaTime;
float t = timer / duration;
//Smoothing algorithm -- Take it out if you don't want it.
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.rotation = Quaternion.Slerp(endRot, startRot, t);
yield return null;
}
}
}
Note that you could remove that second copied While loop by simply reassigning startRot and endRot to each other. I thought you may like to have it like this.
I hope that helps!
Cheers,
~LegendBacon
thank's you! it should works, i'll test it tomorrow, if is possible, can you explain me the algorithm?
Answer by Hellium · Nov 27, 2018 at 02:28 PM
private IEnumerator StartRotation(
Quaternion startRotation,
Quaternion endRotation,
float duration = 1, // duration in seconds
bool iterations = -1 // Infinite
)
{
Quaternion tempRotation ;
if( duration <= Mathf.Epsilon )
yield break ;
while( iterations != 0 )
{
for( float t = 0 ; t < duration ; t += Time.deltaTime )
{
transform.rotation = Quaternion.Slerp(startRotation, endRotation, t / duration);
yield return null;
}
iterations-- ;
tempRotation = startRotation;
startRotation = endRotation;
endRotation = tempRotation;
}
}
Answer by s_awali · Nov 27, 2018 at 02:25 PM
I'm working on a simple class that lets you do such things. Here is a simple version of how I do an action over time :
private IEnumerator SAction(float duration)
{
float percent = 0;
while(percent < 1f)
{
percent += Time.deltaTime * (1 / duration);
percent = Mathf.Clamp(percent, 0f, 1f);
// the action to do, using percent (from 0 -> 1 in "duration" seconds)
}
}
If you want to ping pong, simply do the same, but instead of using "percent", use "1 - percent" (going from 1 to 0).