Animation/tweening effects in code

This is the place to find out how to accomplish something specific with EZ GUI.

Animation/tweening effects in code

Postby Brady » Wed Aug 25, 2010 4:38 am

Here is how to apply various animated effects to an object (whether it is an EZ GUI object or not) from code:
Code: Select all
// GameObject to be animated
var myGameObject : GameObject;

// Rotate an object around the Y axis by 1 degree every second infinitely:
AnimateRotation.Do(myGameObject, // Game Object to animate
   EZAnimation.ANIM_MODE.By, // Animate "by" a certain amount
   Vector3(0, 1, 0), // A Vector3 containing the angles to rotate in each axis
   EZAnimation.elasticOut, // The easing function to use
   -1, // duration of effect, in seconds - negative value means repeat infinitely
   0, // delay before applying effect, in seconds
   null, // no starting delegate
   null); // no ending delegate

// Move an object to a specific point over a period of 2 seconds:
AnimatePosition.Do(myGameObject, // Game Object to animate
   EZAnimation.ANIM_MODE.To, // Animate "To" a certain value/position
   Vector3(1, 2, 3), // The desired destination position
   EZAnimation.elasticOut, // The easing function to use
   2, // duration of effect, in seconds
   0, // delay before applying effect, in seconds
   null, // no starting delegate
   null); // no ending delegate

// Etc...


You will find documentation on other animation classes, as well as other versions of the Do() method, in the EZ GUI scripting reference (the last item on the left-hand navigation panel in the docs). Just look at the EZAnimation class as all such effects are children of this class.

Edit:
Here is a page with most of the same easing types and a good visualization of each:
http://hosted.zeh.com.br/mctween/animationtypes.html
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Animation/tweening effects in code

Postby nafonso » Fri Sep 03, 2010 10:29 am

I'm trying to animate a GO and something is wrong, the 3rd time I call the animation unity hangs (and I'm sure it is not on my code, the only thing called at that time is AnimatePosition). At first I was creating a new AnimatePosition object and then calling Start to execute the animation, and thought that maybe I should be de-initing something that I wasn't, so just to rule that out I called AnimatePosition.Do instead and the same thing happens, the 3rd time it gets called unity hangs. Is this a known issue?

If not, how can I get you a bug report?

Btw, is there a way to know that the animation is running? [EDIT] Nevermind, just saw it by looking at the code, I was only looking for it on the docs, seems to be EZAnimation.running.

[EDIT2] There's something _very_ odd.. Unity was hanging always when I did the 3rd animation. I force quitted it, opened it again, always the same thing. Then I created a new project (btw all this using unity 3.0 beta 7) which only had the animation of the GO, ie not the whole lot of EZGUI objects the other one has, and it worked! The weird thing is that I went back to the other project and now the animation doesn't hang anymore! Like I didn't even change 1 line of code. Maybe the problem was unity compile issues?!
nafonso
 
Posts: 64
Joined: Wed Sep 01, 2010 8:20 pm

Re: Animation/tweening effects in code

Postby Brady » Fri Sep 03, 2010 8:52 pm

I've been running into a couple of really, really weird issues like that with Unity 3, particularly b6 and b7. Like in one case, a function literally exactly like this:

Code: Select all
public bool MyMethod()
{
   return false;
}


...sometimes returns true! Yes, it is hard-coded to be false, yet sometimes it returns true. Absolutely bizarre. And then even stranger, if I re-copy the source file back into the project folder, prompting a re-compile, it goes away. It's got to be something buggy with Unity 3's compiler or something similar, if you ask me.

BTW, you definitely don't want to create EZAnimation objects directly. Just use the .Do() method and if you need to save a reference to the instance of that animation, Do() returns it so you can check things like .running, etc.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Animation/tweening effects in code

Postby patch24 » Tue Nov 09, 2010 7:42 pm

Hi, I was wondering if I could get a code example showing the proper syntax for using the CompletedDelegate and StartDelegate?
I'm new to delegates and have been looking for the correct way to put these into an EZAnimation call.
thanks!
-p
patch24
 
Posts: 12
Joined: Sat Jul 10, 2010 6:48 pm

Re: Animation/tweening effects in code

Postby Brady » Sun Nov 14, 2010 10:49 pm

Sure, no problem:
Code: Select all
// C#
void MyEZAnimDelegate(EZAnimation anim)
{
   Debug.Log("The animation type is: " + anim.type);
}

// JS
function MyEZAnimDelegate(anim : EZAnimation)
{
   Debug.Log("The animation type is: " + anim.type);
}


These delegates will work both as completed as well as start delegates. So you would just type its name (not including parentheses, etc - just the name) in the appropriate argument.

It's worth noting that the delegate receives a reference to the EZAnimation object that is being started/completed. This can come in very useful.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Animation/tweening effects in code

Postby Adsy » Wed Feb 09, 2011 6:04 am

I am having a lot of trouble getting any sort of Transition to work. I have tried a number of different ways, and so far nothing. I'm not getting any errors, it simply doesn't seem to move. Since I want to move a button in from off screen I tried setting up a "MenuManager" GO that simply has a script on it that has a reference to the button, and calls AnimatePosition() on it. It doesn't move, it does set the initial position that I passed into AnimatePosition() but that's it. Since I don't want to have any interaction to bring the button onto the screen I have to script it somewhere but I can't figure out how to do it. Any help would be appreciated.

Just to be clear, I just want the first thing that happens when the game is run is that the menu buttons spring in from off the screen.

Thanks,
Adsy
Adsy
 
Posts: 196
Joined: Wed Feb 09, 2011 5:59 am

Re: Animation/tweening effects in code

Postby Brady » Wed Feb 09, 2011 11:31 pm

Could you post the code you're using? To help keep this sticky thread lean, could you post it to the other thread where you mentioned this?
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Animation/tweening effects in code

Postby Semmerich_HRG » Sun Apr 17, 2011 4:40 pm

Hi guys,

I am new to using EZGUI and I'm trying to animate a GO using AnimatePosition.Do() and GO I'm moving is a child of another GO that is stationary. So far, everything I have done has put the game object at the wrong position. Here is the code:

Code: Select all
   
Vector3 pos1, pos2, pos3;
Transform m_tTransform;
//In start function
void start()
{
m_tTransform = transform;
 
        pos1 = new Vector3(m_tTransform.position.x + 57.0f, m_tTransform.position.y + 37.0f, m_tTransform.position.z + 1.0f);
        pos2 = new Vector3(m_tTransform.position.x - 57.0f, m_tTransform.position.y + 37.0f, m_tTransform.position.z + 1.0f);
        pos3 = new Vector3(m_tTransform.position.x, m_tTransform.position .y - 68.0f, m_tTransform.position.z + 1.0f);

//Creation of Children using instantiate
    Child1 = Instantiate(ChildPrefab, pos1, Quaternion.identity) as GameObject;
    Child1.transform.parent = m_tTransform;
   Child1.name = CHILD_NAME1;

  Child2 = Instantiate(ChildPrefab, pos2, Quaternion.identity) as GameObject;
    Child2.transform.parent = m_tTransform;
   Child2.name = CHILD_NAME2;

  Child3 = Instantiate(ChildPrefab, pos3, Quaternion.identity) as GameObject;
    Child3.transform.parent = m_tTransform;
   Child3.name = CHILD_NAME3;
}




//Function to move them
void Move()
{
   AnimatePosition.Do(Child1, EZAnimation.ANIM_MODE.To, (pos2), EZAnimation.GetInterpolator(EZAnimation.EASING_TYPE.Linear), swapTime, 0.0f, null, EndSwap);

   AnimatePosition.Do(Child2, EZAnimation.ANIM_MODE.To, (pos3), EZAnimation.GetInterpolator(EZAnimation.EASING_TYPE.Linear), swapTime, 0.0f, null, EndSwap);

   AnimatePosition.Do(Child3, EZAnimation.ANIM_MODE.To, (pos1), EZAnimation.GetInterpolator(EZAnimation.EASING_TYPE.Linear), swapTime, 0.0f, null, EndSwap);
}
   


Doing the Above buts them at the correct place if they were not children of the other object. Ie. if pos1 is -56, 270, 0 the object ends up there in world space, but if I were to do the same movement myself with a lerp or just set the position it would end up in the correct position based on its parent. Ie. the same as subtracting pos1 from the parents position then doing the code above. This honestly makes no sense to be shouldn't they both be the same?

I'm very confuse and I hope that what I said makes sense and any help you guys can give would be greatly appreciated.

Thanks

SE_HRG
Semmerich_HRG
 
Posts: 3
Joined: Sun Apr 17, 2011 4:22 pm

Re: Animation/tweening effects in code

Postby Brady » Sun Apr 17, 2011 7:29 pm

AnimationPosition operates on the object's localPosition, not its world position (.position), so I'm thining the problem is that you seem to be passing world space positions, when you need to be passing local space instead.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Animation/tweening effects in code

Postby Semmerich_HRG » Mon Apr 18, 2011 10:46 pm

Thanks for the response Brady. So I changed that to use local positions like you said and that one moves fine but now I have another issue related to AnimatePoistion. I am trying to move two diferent objects to each others positions. Ie. swapping them. so I did what you said and passed the function the objects localposition. doing that translate it in the right direction its just really far away from where it is supposed to be visually. I am having a really hard time understanding how the animateposition works because everything I think should work or works when I do the translation myself doesn't work. Here's some code that will hopefully help explain my problem:
Code: Select all
   //Function swaps pieces
   public void Swap(string _Obj1Name, string _Obj2Name)
   {
      // If are selected
      if (!Swapping)
      {
         if (Selected)
         {
            if (_Obj1Name == name)
            {
               // Set our position to swap to
               m_SwapPiecePos = GameObject.Find(_Obj2Name).transform.localPosition;
               // Set Swapping to true
               Swapping = true;
            }
            else
            {
               // Set our position to swap to
               m_SwapPiecePos = GameObject.Find(_Obj1Name).transform.localPosition;
               // Set Swapping to true
               Swapping = true;
            }
            
            AnimatePosition.Do(gameObject, EZAnimation.ANIM_MODE.FromTo, transform.localPosition, m_SwapPiecePos, EZAnimation.GetInterpolator(EZAnimation.EASING_TYPE.Linear), m_fSpeed, 0.0f, null, ShowComplete);
         
         }
      }
   


Maybe I'm just incompetent at programming but this is very frustrating to me.

Any help would be great.
thanks
Semmerich_HRG
 
Posts: 3
Joined: Sun Apr 17, 2011 4:22 pm

Next

Return to EZ GUI How to...

Who is online

Users browsing this forum: No registered users and 2 guests