- Home /
How would I go about moving a group of objects as one using iTween?
Well. What I actually have at the moment is a trigger to create a trigger, which when entered needs to move a group of objects as one across the floor. I just don't know how to achieve this with iTween. Would I create many variables and use assign a game object to each, or would I group them all as one game object and then use iTween to move them?
Thanks :D
Answer by DannyB · Jun 16, 2013 at 07:25 AM
There are several ways to do it, each more suitable to different situations.
Option 1: Grouped Objects
If it makes sense to have the objects you are about to move parented to a single object throughout the game, then you should consider doing it. For example, if you want to move the entire background, have it pre-grouped under one object.
Option 2: Objects are responsible for moving themselves
In some cases, you would make your objects respond to some event, and they will activate their own iTween on themselves.
Option 3: Centralized controller moving other objects
This situation may work when you want to move all objects that are tagged the same, or that you already have a reference to.
Option 4: Grouping on demand
May be suitable for some cases - this is the same as the previous option, only instead of calling iTween for each object, you first parent them and then use one iTween on the parent.
All in all, your question is actually not only iTween related, but it seems like you are trying to figure out a way to handle multiple objects in the same way. This is a different question.
I am usually using option 2.
EDIT:
Skeleton for implementing event listeners
A little more details about option 2 as requested. This is one way of doing it, there are more of course, depending on the situation.
The object that controls this movement, declares an event:
public class MyController {
public delegate void BangEvent( float direction );
public static event BangEvent onBang;
...
Then, whenever you want to trigger (raise) this event (from the same object), you do:
if( onBang != null ) onBang( direction )
Finally, your other objects (ones that should move), subscribe to this event.
void OnEnable() {
MyController.onBang += OnBang;
}
void OnDisable() {
MyController.onBang -= OnBang;
}
void OnBang( float direction ) {
iTween.MoveBy( gameObject, ... );
}
So if I group them all to a specific object (say, an empty game object), will this mean that I can declare my gameObject variable the empty game object, and, in turn, the objects grouped with it?
Yes. Grouping them under an empty object and then iTween.$$anonymous$$oveTo( thatEmptyGameObject, ... ) will move all of its children with it.
I see. And your option 2, how would you go about doing this?
I like option 2. I have extended the answer, but this is a whole other topic... :)
Your answer
Follow this Question
Related Questions
Can I move a component from one GameObject to another in script? 1 Answer
Trigger moves another object 1 Answer
Object moving OnTriggerEnter, please help?! 2 Answers
Moving object with a trigger? 0 Answers
check if object is moving 5 Answers