FixedUpdate in Coroutine
Hi, I'm trying to do something like a Blink/Teleport effect, but I'm facing some problems with Coroutine and FixedUpdate.
I have a Joint based Hair in a character, so I need to use FixedUpdate to move the character controller or they will desync, and to make the effect I'm using particle effect, that at void Start() I stop the emission fo particles and make a Coroutine like this:
IEnumerator Shift_Skill()
{
float t = 0;
float TPDistance = 20;
Vector3 storedPos = transform.position;
canControl = false;
shiftSkillCD = shiftSkillCDMax;
trailEffect.Play();
yield return new WaitForFixedUpdate(); //Time to trail start
characterController.enabled = false;
if (Physics.Raycast(transform.position, model.transform.TransformDirection(Vector3.forward), out hit, TPDistance))
{
while (t < 1)
{
t += Time.deltaTime / 0.2f;
transform.position = Vector3.Lerp(storedPos, storedPos + (model.transform.TransformDirection(Vector3.forward) * (hit.distance - 1)), t);
cameraFollow.transform.position = new Vector3(0, 0, transform.position.z);
yield return new WaitForFixedUpdate();
}
}
else
{
while (t < 1)
{
t += Time.deltaTime / 0.2f;
transform.position = Vector3.Lerp(storedPos, storedPos + (model.transform.TransformDirection(Vector3.forward) * TPDistance), t);
cameraFollow.transform.position = new Vector3(0, 0, transform.position.z);
yield return new WaitForFixedUpdate();
}
}
yield return new WaitForFixedUpdate();//Time to trail get to the point
trailEffect.Stop();
characterController.enabled = true;
canControl = true;
}
The problem is when I execute this coroutine, this is running at normal Update and not FixedUpdate, and after the Blink/Teleport the Hair Desync with the body. In this code, I'm using yield return new WaitForFixedUpdate() because without it the code runs so fast that there's no time enough for the particle effect Start, resulting in it not initializing at all. That's why I need to wait for one frame right after Start and Before close.
I tried it without coroutine, direct in the FixedUpdate, but I didn't find a way to "wait for one frame" in the FixedUpdate, also tried a coroutine that only waits for one frame and them change a bool, and this bool release the rest of code, but didn't work.
I'm out of Idea and searched, but I didn't see anyone with a problem similar to mine. I also tried things like "yield return null" and "yield return 0", but didn't work either. So if anyone knows what I can do to make this work, I'll appreciate, in advance, my thanks.
Answer by K-Skanz · Sep 30, 2020 at 11:24 PM
Tl;dr I need to run Coroutine with FixedUpdate frequency, And I'm not figured out how to do it yet.
Your answer
Follow this Question
Related Questions
How to set ArticulationBody motion to "limited" in C# 0 Answers
Unity 3D Balloons tied together (Physics) 0 Answers
Jumping with rigid body velocity doesn't feel good 1 Answer
How to Properly Move Two Attached Kinematic Rigidbodies 1 Answer
Attach Kinematic Collider To Non-Kinematic Collider 1 Answer