Search Unity

iTween targeting different gameobjects

Discussion in 'Scripting' started by andy_h, Jan 24, 2011.

  1. andy_h

    andy_h

    Joined:
    Nov 3, 2009
    Posts:
    146
    I've just started using iTween, which has solved a lot of animation problems I was having.

    However I was wondering if there is a way to talk to different gameObjects with one script?

    To explain further, I have several monsters that move up and down. Each is a prefab with one script that tells them how to explode and collide give scores etc. I have the following code to make them move up and down :
    Code (csharp):
    1. function Update ()
    2. {
    3. iTween.MoveTo(gameObject,{"y":4.25,"time":2,"easetype":iTween.EaseType.linear,"looptype":iTween.LoopType.pingPong});
    4. }
    5.  
    this however moves all the monsters up and down to the same coordinates. So would like to give each gameobject a name like enemy1, enemy2 etc and then call each object with a different line of code for the right coordinates for tht gameobject.

    Thanks
     
  2. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    Code (csharp):
    1.  
    2. var target: GameObject;  //One of your many monsters.
    3. var eventName : String;  // One of your various iTweened monster coordinates.
    4.  
    5. function OnTriggerEnter (other: Collider){ //or which ever function type you need
    6.          
    7.             iTweenEvent.GetEvent(target,eventName).Play();
    8.        }
    9.  
    attach this to each monster.
    attach the GameObject (variable)
    and type in the name of your event.(string)
    This snippet assumes that you are also using the iTween Visual Editor.
    Hope this helps
    K
     
    Last edited: Jan 24, 2011
  3. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    If you want to do it the hard way and script in every iTween then it may look something like this

    Code (csharp):
    1.  
    2. var target :GameObject; //one of your monsters.
    3.  
    4. function Update ()
    5. {
    6. iTween.MoveTo(target,{"y":4.25,"time":2,"easetype":iTween.EaseType.linear,"looptype":iTween.LoopType.pingPong});
    7. }
    8.  
    attach to monster.
    attach the GameObject (variable)

    hope this works.
    K
     
    Last edited: Jan 24, 2011
  4. andy_h

    andy_h

    Joined:
    Nov 3, 2009
    Posts:
    146
    Thanks shabazzster,

    I've not tried the visual editor yet, but the code is working great. Thanks for you help :)