- Home /
For loop with iTween c#
A picture is worth a thousand words, so I attached one for better understanding my problem. So, for the first option, I figured it out, how to animate boxes in desired order. Here's the code:
for (int i = 0; i < rowsMinus; i++)
{
print(i);
for (int j = 0; j < rowsMinus; j++)
{
iTween.MoveTo(box[j], iTween.Hash("z", rows[j].transform.position.z + 80, "time", 0.7, "easetype", "easeInOutQuad", "looptype", iTween.LoopType.none));
print(j);
if (i == j)
{
break;
}
}
yield return new WaitForSeconds(0.7f);
}
For 2 option, I'm stucked. The question is, how to make an reverse animation with for loop of four boxes in order that I gave you in attached image?
Thank u for your answer!
Kj
Answer by robertbu · Oct 04, 2014 at 05:49 PM
You need a way to calculate the positions, and you need to walk your array of boxes in reverse. Here is a bit of example code. Since your is pulled from a larger context, you'll have to examine this code and then rewrite your code:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public GameObject[] boxes;
public float dist = 1.1f; // Distance between pivots unstacked
void Update() {
if (Input.GetKeyDown (KeyCode.Space)) {
StartCoroutine (Unstack());
}
}
IEnumerator Unstack() {
float posX = boxes[0].transform.position.x + (boxes.Length - 1) * dist;
for (int i = boxes.Length - 1; i > 0; i--) {
iTween.MoveTo(boxes[i], iTween.Hash("x", posX, "time", 0.7, "easetype", "easeInOutQuad", "looptype", iTween.LoopType.none));
yield return new WaitForSeconds(0.7f);
posX -= dist;
}
}
}
hi. It's not working as it should. It moves boxes but not the way I would like. Something is missing. Your code moves boxes all in one sequence. I would like to move boxes in four sequences. In the first sequence move all of them. In the second the last one stays and the others move. In next step, we move only two boxes, in next one last one. like I showed on attached image on option 2. Am I clear?
I marked your answer, because my attached image was a little bit disturbing. And your code does what I was asking for. The thing is I didn't asked right! In comment above I tried to explain what is the real animation scenario.
I'm sorry I was unable to give you what you wanted. Your comment is still a bit confusing since it appears that the object are moving to the left (based on your comment) but your arrows are pointing to the right?
Answer by Kjurek · Oct 06, 2014 at 08:18 AM
Hi there. So, I figured it out, how to make it the way I would like to. As you can see in my first option code you just have to do another for loop like this:
for (int j = boxes.Length; j > 0; j--)
{
for (int i = 1; i <= boxes.Length; i++)
{
iTween.MoveTo(boxes[i], iTween.Hash("z", boxes[i].transform.position.z - 80, "time", 0.7, "easetype", "easeInOutQuad", "looptype", iTween.LoopType.none));
if (i == j)
{
break;
}
}
yield return new WaitForSeconds(0.7f);
}
Your answer
Follow this Question
Related Questions
Issues with Inventory script 0 Answers
Cloud recognition in Vuforia 0 Answers
Shorten code with FOR loop 1 Answer
Itween CRSpline to BezQuad 2 Answers
Android multitouch problem 1 Answer