- Home /
Waiting between pingpong loops
I am using iTween to move an object left to right and vice versa. But I want it to where it reaches its destination, stops for a few seconds and then it continues on its path.
iTween.MoveTo(_cubeObject, iTween.Hash("name", "Xplane", "position", new Vector3(1, 1, 0), "speed", speed,"easetype", iTween.EaseType.linear, "looptype", iTween.LoopType.pingPong));
I was told by someone in the Unity Community to make this method:
IEnumerator waitTime(float waitTime)
{
yield return new WaitForSeconds(waitTime);
}
and then simply call it where I want it to stop. But the thing is that I wrote it below the itween line and nothing happens. The object keeps sliding across the world.
Can anyone help me figure this one out? Thanks in advance!
Answer by GameVortex · Jan 07, 2014 at 03:04 PM
ITween has a callback parameter for when the itween has finished. When the looptype is set to PingPong the callback will be called everytime the target reaches the start and when it reaches the end of the tween. So you can include the callback to a function that pauses the tween, waits and then continues the tween. Here is an example:
private void Start()
{
//Notice the oncomplete at the end. The "oncomplete" references the function it will call, and "oncompletetarget" takes the GameObject it will call the function on (actually SendMessage).
iTween.MoveTo(_cubeObject, iTween.Hash("name", "Xplane", "position", new Vector3(1, 1, 0), "speed", speed,"easetype", iTween.EaseType.linear, "looptype", iTween.LoopType.pingPong, "oncomplete", "PauseTween", "oncompletetarget", gameObject));
}
private void PauseTween()
{
StartCoroutine(PauseTween(3));
}
private IEnumerator PauseTween(float waitTime)
{
//Pause all itweens on target object
iTween.Pause(_cubeObject);
yield return new WaitForSeconds(waitTime);
iTween.Resume(_cubeObject);
}
This will start the coroutine when the tween reaches the end, pause the tween for 3 seconds and resume the tween again. Keep in mind that this will stop it when it gets back to the start as well.
Answer by mischinab · Oct 08, 2018 at 03:03 PM
In the iTween.hash
you can use add a parameter "delay", seconds
to specify how long to pause between each iteration (e.g. before the ping, and before the pong).
Your answer
Follow this Question
Related Questions
Mysteries of yield 1 Answer
How can I make the health points constantly go down? 1 Answer
Only the first half of my coroutine works 1 Answer
WaitForSeconds not running 2 Answers
WaitForSeconds while having an Update/CoUpdate in C# 1 Answer