Condition implementation Problem
Just take this example :
void Start ()
{
StartCoroutine("TriggerCubeAnimation");
}
IEnumerator TriggerCubeAnimation()
{
foreach (var o in cubes)
{
while (o.transform.position.y<fMaxLimit)
{
o.transform.Translate(v3speed.x * Time.deltaTime, v3speed.y * Time.deltaTime , v3speed.z * Time.deltaTime);
Debug.Log(v3speed.x*Time.deltaTime + " " + v3speed.y*Time.deltaTime + " " + v3speed.z*Time.deltaTime);
yield return null;
}
}
yield return new WaitForSeconds(1.0f);
}
I have Four Cubes in Row what i want is when my first cube start animation and when it reached at position 3.0f at Y-axix then the other cube start its animation all four cubes reached at 3.0Y in a row . like first then second then third and then fourth. and it does. But problem start when i want to make it faster if i make v3speed.y a big value it over reached the while condition and give some thing weired values.
for example : if fMaxLimit is 3.0f. then if i make v3speed.y = 20.0f the v3speed.y increses 20 per/sec and
the cube not stops of 3.0 they always stop like 3.22225 , 3.125654 , 3.56565848 Some thing greater than 3.0
Answer by JedBeryll · May 26, 2016 at 07:34 PM
If you give it large steps like that it will of course overshoot, you never tell it to stop at 3. for example an iteration calculates v3speed.y * Time.deltaTime which is let's say 0.524f, and v3speed.y was already at 2.9f it will be 3.424. If you want to keep it from overshooting you can clamp it's magnitude with Mathf.clamp, or calculate the next value without assigning it and check if it is larger than the fMaxLimit. If it's larger then just make it equal fMaxLimit. Otherwise Translate it.
Your answer
Follow this Question
Related Questions
Coroutine doesn't continue after yield another coroutine 0 Answers
Coroutine or While loop that you can reset/add time too? 0 Answers
Designing a dialogbox that displays message, returns a custom object and waits for user response 0 Answers
Failing a UnityTest when a condition that's expect to change in given amount of time doesn't? 1 Answer