Reputation: 10561
I want to do
And I have two ways to acheive this thing(means to run my logic in).
for coroutine I used this
IEnumerator IncreaseSpeedGradually1(AnimationControlSpeed lastGOHitScript)
{
//stop if decrease speed in progress
StopCoroutine("DecreaseSpeedGradually");
float decrementValue = ((lastHitVehicleSpeed / 2) * 2);
while (lastGOHitScript.Speed <= lastHitVehicleSpeed)
{
lastGOHitScript.Speed += decrementValue * Time.deltaTime;
yield return 0;
}
//setting speed to the last speed
lastGOHitScript.Speed = lastGOHitScript.iniSpeeed;
}
While for update I just make this criteria:
if (carAnimState == carAnimationState.starting)
{
carAnimState = carAnimationState.running;
}
if (carAnimState == carAnimationState.stoping)
{
carAnimState = carAnimationState.running;
}
These two ways I know but i want to ask that which is right way to do this job? to slow down animation speed and hence get my objective? I guess corroutine later can be problematic in my game and what are the performance concerns?
Upvotes: 1
Views: 353
Reputation:
Not perfectly clear what your goal exactly is. What you describe is clear, but the two code snippets you posted do completely different things. BUT, basically, for something this small I would use Update() as it's easier to debug if something blows up and there's nothing -theoretically- in your code that wishes "threading" / "side tasking" (which coroutines are for, basically: do something -most probably a side task- async).
As a side note, the code in your Update() can be optimized a little so it will look something like this:
if (carAnimState == carAnimationState.starting) {
carAnimState = carAnimationState.running;
} else if (carAnimState == carAnimationState.stoping) {
carAnimState = carAnimationState.running;
} //this way the 2nd "if" won't be unnecessarily evaluated when animstate was .starting
I'd also add a method instead and call that from Update(). The pre-compiler will inline it most probably (cannot be forced in Unity) and your code is more readable.
Upvotes: 1