How to make fast a coroutine
private IEnumerator SwipeLeftPanel(object[] parms)
{
// float right = -PanelRectTransform.offsetMax.x;
float tempLeft = (float) parms[0];
float left = PanelRectTransform.offsetMin.x;
// float tempRight = (float)parms[1];
while (left >= tempLeft)
{
left = left - subPeriod;
PanelRectTransform.offsetMin = new Vector2(left,PanelRectTransform.offsetMin.y);
Debug.Log("left"+left);
yield return null;
}
}
StartCoroutine("SwipeLeftPanel", parms);
I need to move my panel to left side so I give it left and right margin but increment of 1.0f so it slowly move to left and stop when I reaches at certain left and right margin position . left is the amout every time panel move to left.but I want it faster how to do it .thanks in advance :)
Can you just increase the amount you move it each frame?
Answer by Dave-Carlile · Oct 29, 2015 at 03:37 PM
yield return null
stops the Coroutine until the next frame, which means this is doing one iteration of the loop each frame, which is the same thing you get with Update. So the Coroutine itself is already looping as fast as it can.
So what you need to do is subtract more from left
each frame so the panel moves further each frame. A common way to do this sort of thing is to define a speed
value that is set to the number of units to move each second
. So setting speed to 10...
float speed = 10.0f;
We would interpret to mean "Move 10 units each second". Now to apply that value we need to scale it by how long it's taking to render a frame using Time.deltaTime
. That value is in seconds. By multiplying Time.deltaTime
by speed
we get the number of units we need to move for an individual frame such that over 1 second we'll have moved 10 units...
left = left - speed * Time.deltaTime;
So if we're rendering at 60FPS, Time.deltaTime will generally have the value 1/60 or 0.016667. Multiplying that by 10 gives us 0.16667, which is the value we'll subtract from left every frame. If you add that up over 60 frames you'll get to 10. In other words it will take 60 frames to move 10 units, and it will take 1 second to do so.
The nice thing with this setup is that you only really have to be concerned with the value for speed
and it's easy to conceptualize "x units per second". So if you want the object to move faster just increase the speed, or if it's too fast then decrease the speed.
Your answer
Follow this Question
Related Questions
Text Component Size is not Adjusting 1 Answer
Player character jumping when I wish to only initiate pause - UI Button element. 1 Answer
Slider.value doesn't show the value in real-time but after the execution of the script 1 Answer
UI inputfield not shows mobile keyboard on click 0 Answers
Overlap ScrollRect 1 Answer