Coroutines are not appropriate for this since conceivably two could be started simultaneously and fight each other in unpredictable ways.
All you need is this value-smoothing pattern:
Smoothing movement between any two particular values:
The problem is the choice of integer values for lanes. They can only either be lane 0, 1 or 2. They can’t be in between round numbers. It needs to be a floating point value so that it can be between lanes.
This type of question comes up so universally I have finally just made a sample package to demonstrate the basic concept. Read my first post above and see if you can correlate each part of the code to the steps indicated.
Code located here: SmoothMovement.cs · GitHub
6430100–719246–SmoothMo…
You have currentQuantity and desiredQuantity.
only set desiredQuantity
the code always moves currentQuantity towards desiredQuantity
read currentQuantity for the smoothed value
Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.
The code: SmoothMovement.cs · GitHub
EDIT: This character controller does not use coroutines for anything. It supports crouching and a lot of other features too: run, walk, jump, slide, etc.
https://www.youtube.com/watch?v=t0oN-PFXZEM
Gist Link
After some interest in an old post here , I spent the weekend frankensteining snippets of code to make a single script FPCC that could be used like the FPCC from Unity 4~5, including the crouch and run from Aldo, and I also added farcry sliding. There is a Menu Item function to easily create and configure a new FPCC object. Otherwise very easy to set up, it only requires a layer to exclude itself from sphere/raycasts. There is a check to …
More on coroutines:
Coroutines in a nutshell:
Hey Epoch, here’s how it goes down. A coroutine is just an IEnumerator object, and you make one by just calling a coroutine.
When you do that, nothing happens. It’s like a water pump that needs to have its crank pumped, but nobody is pumping it so it sits there and does nothing.
In Unity there’s a special agreement: if you hand that IEnumerator object into StartCoroutine(), which is a method belonging to the object where that StartCoroutine() call is located, what Unity does is starts “pumpin…
Coroutines are an IEnumerator object. They have their own internal state. It’s different from just calling InvokeRepeating(), which would restart a fresh function call each time.
Here is ALL you need to know about coroutines in Unity:
they are an IEnumerator, also known in some languages as a Generator. It is an object with a .MoveNext() method on it that “yields” the next thing with each subsequent call. That is what you create by calling an IEnumerator: you create the object, you do NOTHIN…
Splitting up larger tasks in coroutines:
Technically, yeah, there’s always a way. But in practice, in the general sense it can get really hairy.
Also, in the particular case of adding lots of GameObjects, I know they’re not linear: last I checked, adding 1000 GameObjects is WAY more than 10x slower than adding 100 Gameobjects, I presume due to inefficiencies in newly-added stuff, not sure/ No way to tell with a closed-source engine like Unity.
Trying to tie into system timers and judge how long things have taken so far will get you …
Coroutines are NOT always an appropriate solution: know when to use them!
“Why not simply stop having so many coroutines ffs.” - orionsyndrome on Unity3D forums
The main thing to check before calling / making a coroutine is to ask yourself,
“Does this even need to be a coroutine?”
Generally, if your design requires you to call StopCoroutine(), then don’t make it a coroutine.
If you just need something to happen a bit later, this is a more-robust approach:
https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e
If you’re flipping from one value to another, such as fading in, fading out, changing speeds slowly, etc., coroutines are an AW…
There’s never a need. As @PraetorBlue pointed out in a recent post of his, nobody NEEDS coroutines.
They do solve a certain class of problem nicely however, primarily problems of sequencing a set of data or events to happen.
Coroutines CAN be interrupted and restarted but doing so exponentially increases your chance of a bug, negating a lot of their inherent benefits.
A lot of human behaviors are good coroutines: “Go take a shower.”
So that breaks down to:
get undressed
start the shower
…
Our very own Bunny83 has also provided a Coroutine Crash Course:
https://answers.unity.com/questions/1749615/coroutines-ienumerator-not-working-as-expected.html?childToView=1749714#answer-1749714