- Home /
better way to rotate instead Coroutine
hello! i hope somebody can help me to figure out how to make my code better...
i want to be able to rotate my object of 360 degrees, here what i have now
private IEnumerator FullSwingCoroutine(
Quaternion start,
float in_deltatime,
int id)
{
Vector3 startingRot = start.eulerAngles;
float difference = Mathf.Abs(_playerAngle[id] + 180);
float end = startingRot.x + 360.0f + difference;
float t = 0.0f;
const float duration = 2.0f;
Quaternion finalRotation = Quaternion.Euler(-180,0.0f,90.0f);
Vector3 final = finalRotation.eulerAngles;
while (t < duration)
{
if(_stopRotation[id])
{
yield break;
}
t += Time.deltaTime;
float time = t / duration;
float angle = Mathf.SmoothStep( startingRot.x,end,time );
Vector3 fixedRot = new Vector3 (angle,startingRot.y,startingRot.z );
_playerAngle[id] = -angle + 180.0f;
_players[id].transform.rotation = Quaternion.Euler( fixedRot );
yield return null;
}
}
but as you can see i want to be able to stop then restart anytime i want, i have one big problem here, because if i don't do something like this.
private IEnumerator FullSwingCoroutine(
Quaternion start,
float in_deltatime,
int id)
{
Vector3 startingRot = start.eulerAngles;
float difference = Mathf.Abs(_playerAngle[id] + 180);
float end = startingRot.x + 360.0f + difference;
if (end > 720)
{
end = 720;
}
float t = 0.0f;
const float duration = 2.0f;
Quaternion finalRotation = Quaternion.Euler(-180,0.0f,90.0f);
Vector3 final = finalRotation.eulerAngles;
while (t < duration)
{
if(_stopRotation[id])
{
yield break;
}
t += Time.deltaTime;
float time = t / duration;
float angle = Mathf.SmoothStep( startingRot.x,end,time );
Vector3 fixedRot = new Vector3 (angle,startingRot.y,startingRot.z );
_playerAngle[id] = -angle + 180.0f;
_players[id].transform.rotation = Quaternion.Euler( fixedRot );
yield return null;
}
}
end variable will increase then i'll get more than just 360 degrees.
another things is Coroutine... i'm not like it so much.. i want to do it in a method so i'll be able to stop then restart it anytime i want.
anyone can help me to make it better and clean?
Answer by L42yB · Jan 10, 2019 at 05:52 PM
You can stop and restart coroutines.
Coroutine foo = StartCoroutine(YourFunction());
// Then whenever you want
if(foo != null)
StopCoroutine(foo);
The coroutine object will become null when the coroutine finishes. You can repeat as necessary.
Tthank’s you for your answer.
I’m not able to do that because when i’m trying to start coroutine an error message say that i can’t confrrt from IEnumerator to coroutine
Ah yes sorry you are right. You can only stop it with the Coroutine object.
You can just restart it by doing this again: StartCoroutine(YourFunction());
I will update my answer
Updated it. You should check for nulls on the coroutine object as when the coroutine is finished it will be set to null.
To trigger it again, just call StartCoroutine again.
hi! this should work, but as i wrote i need something that i would be able to stop also before it finish, so i must be able to stop coroutine wherever i want then restart it again
As long as you save the Coroutine object when you start it (maybe make it a property on the class) then you can stop it whenever you need to using that reference.
You can tell if it is still running or not by checking if it is null.
You can stop the coroutine from within the IEnumerator code by just using yield break;
Or from outside the IEnumerator code with StopCoroutine