AniMate

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
Line 4: Line 4:
 
== Description ==
 
== Description ==
  
Complex animations are best done using Unity's animation system and bones. But there are times when you just need to move a block or during rapid prototyping when you don't want to bother a 3d application. Unity provides some functions for those cases like Lerp, Repeat, PingPong etc. They are fine and can be very powerful using coroutines. However, inspired by TweenLite library from Flash I wanted a simpler approach that would allow me to dispatch an animation and not needing to bother futher while also making my code a bit simpler.
+
Complex animations are best done using Unity's animation system and bones. But there are times when you just need to move a block or during rapid prototyping when you don't want to bother a 3d application. Unity provides some functions for those cases like Lerp, Repeat, PingPong etc. They are fine and can be very powerful using coroutines. However, inspired by TweenLite library from Flash, I wanted a simpler approach that would allow me to dispatch an animation and not have to bother with it further, while also making my code a bit simpler.
  
 
=== Features ===
 
=== Features ===
Line 14: Line 14:
 
* Animate rotation
 
* Animate rotation
  
== Usage ==
+
== Installation ==
  
=== Quick Introduction ===
+
To install AniMate, simply import the Ani.boo script into your ''Standard Assets/Scripts'' folder. AniMate is then available from any script outside of the Standard Assets folder and from any Boo script inside it.
  
Animations are created with '''To''' and '''From'''. Both methods take three arguments:
+
=== Download ===
 +
 
 +
Download AniMate here: [http://forum.unity3d.com/viewtopic.php?t=12413]
 +
 
 +
=== Version History ===
 +
 
 +
==== v1 (4. July 2008) ====
 +
 
 +
* Initial release
 +
 
 +
== Quick Introduction ==
 +
 
 +
Animations are created with [[#To|'''To''']], [[#From|'''From''']] and [[#By|'''By''']]. All three methods take three arguments:
 
# The object you want to animate properties on (''this'' for current object)
 
# The object you want to animate properties on (''this'' for current object)
 
# The duration of the animation in seconds
 
# The duration of the animation in seconds
# A Hash containing the properties to be animated as keys plus the target as value. The Hash can also contain AniMate options.
+
# A Hash containing the properties to be animated as keys and the target as value. The Hash can also contain AniMate options.
  
 
For example, to move a game object to another position:
 
For example, to move a game object to another position:
Line 28: Line 40:
 
<javascript>Ani.Mate.To(gameObject.transform, 2, {"position": new Vector3(0,20,0), "localScale": new Vector3(2,2,2)});</javascript>
 
<javascript>Ani.Mate.To(gameObject.transform, 2, {"position": new Vector3(0,20,0), "localScale": new Vector3(2,2,2)});</javascript>
  
=== Methods ===
+
== Methods ==
  
 
AniMate is available from any script using Ani.Mate. It provides following methods:
 
AniMate is available from any script using Ani.Mate. It provides following methods:
  
==== To ====
+
=== To ===
  
 
* '''To'''(object '':Object'', duration '':float'', properties/options '':Hashtable'') '':WaitForSeconds(duration)''
 
* '''To'''(object '':Object'', duration '':float'', properties/options '':Hashtable'') '':WaitForSeconds(duration)''
Line 38: Line 50:
 
Create an animation from the current value to the given value.
 
Create an animation from the current value to the given value.
  
<javascript>Ani.Mate.To(transform, 3, {"position": new Vector3(10,0,0)});</javascript>
+
<javascript>// Move object from it's current position to (10,0,0)
 +
Ani.Mate.To(transform, 3, {"position": new Vector3(10,0,0)});</javascript>
  
==== From ====
+
=== From ===
  
 
* '''From'''(object '':Object'', duration '':float'', properties/options '':Hashtable'') '':WaitForSeconds(duration)''
 
* '''From'''(object '':Object'', duration '':float'', properties/options '':Hashtable'') '':WaitForSeconds(duration)''
Line 46: Line 59:
 
Create an animation from the given value to the current value.
 
Create an animation from the given value to the current value.
  
<javascript>Ani.Mate.From(transform, 3, {"localScale": new Vector3(0,0,0)});</javascript>
+
<javascript>// Move object from (0,0,0) to it's current position
 +
Ani.Mate.From(transform, 3, {"localScale": new Vector3(0,0,0)});</javascript>
  
==== By ====
+
=== By ===
  
 
* '''By'''(object '':Object'', duration '':float'', properties/options '':Hashtable'') '':WaitForSeconds(duration)''
 
* '''By'''(object '':Object'', duration '':float'', properties/options '':Hashtable'') '':WaitForSeconds(duration)''
Line 54: Line 68:
 
Create an animation from the current value and animate by the given value.
 
Create an animation from the current value and animate by the given value.
  
<javascript>Ani.Mate.By(transform, 3, {"localScale": new Vector3(10,0,0)});</javascript>
+
<javascript>// Move object by 10 units on the x axis
 +
Ani.Mate.By(transform, 3, {"localScale": new Vector3(10,0,0)});</javascript>
  
==== Stop ====
+
=== Stop ===
  
 
* '''Stop'''(object '':Object'', name '':string'')
 
* '''Stop'''(object '':Object'', name '':string'')
Line 63: Line 78:
 
<javascript>Ani.Mate.Stop(transform, "position");</javascript>
 
<javascript>Ani.Mate.Stop(transform, "position");</javascript>
  
==== StopAll ====
+
=== StopAll ===
  
 
* '''StopAll'''(object '':Object'')
 
* '''StopAll'''(object '':Object'')
Line 70: Line 85:
 
<javascript>Ani.Mate.StopAll(transform);</javascript>
 
<javascript>Ani.Mate.StopAll(transform);</javascript>
  
=== Options ===
+
== Options ==
  
The hash that is given to '''To''' and '''From''' can contain some special options. Options can also be passed to those methods as seperate hash for applying the same options to multiple animations or to avoid name conflicts.
+
The hash that is given to [[#To|'''To''']], [[#From|'''From''']] and [[#By|'''By''']] can contain AniMate options.
 +
Options can also be passed to those methods as separate hash for applying the same options to multiple animations or to avoid name conflicts.
  
==== Easing ====
+
=== Easing ===
  
* {"'''easing'''": ''SimpleEasing or ComplexEasing class''}
+
* {"'''easing'''": ''AnimationEasing class''}
Easing allows to apply easing to an animation. LinearEasing is the default and means that the change will be constant over the whole animation. Different easings can be chosen to make the animation start fast and the slow down on approach it's target.
+
Easing allows to make an animation non-linear. LinearEasing is the default and means that the change will be constant over the whole animation. Different easings can be chosen to, for example, make the animation start fast and the slow down when it approaches it's target.
See ''Easing Classes'' for what different kinds of easing are available.
+
See [[#Easing Classes|''Easing Classes'']] for what different kinds of easing types are available.
  
==== Easing Direction ====
+
=== Easing Direction ===
  
 
* {"'''direction''': EasingType.In / EasingType.Out / EasingType.InOut}
 
* {"'''direction''': EasingType.In / EasingType.Out / EasingType.InOut}
Easing also has a direction that's either In, Out or InOut. In generally means the animation starts fast and then slows down, Out means the animation starts slow and and ends fast and InOut is the combination of both.
+
Easing also has a direction that's either ''In'', ''Out'' or ''InOut''. ''In'' generally means the animation starts fast and then slows down, ''Out'' means the animation starts slow and and ends fast and ''InOut'' means the animation starts slow, is fastest at halfway and then slows down again.
  
==== Delay ====
+
=== Delay ===
  
* {"'''delay''': true / false}
+
* {"'''delay''': ''delay in seconds''}
 
If you don't want an animation to start immediately, you can delay the animation with this option.
 
If you don't want an animation to start immediately, you can delay the animation with this option.
  
==== Physics ====
+
=== Rigidbody ===
 +
 
 +
* {"'''rigidbody''': ''Rigidbody instance''}
 +
Shorthand for animating rigidbodies. This makes sure the rigidbody is animated so that it correctly interacts with other physics objects. This option only applies to "position" and "rotation" properties.
 +
This is equivalent to {''physics'': true, ''callback'': rigidbody.MoveXX}.
 +
 
 +
=== Physics ===
  
 
* {"'''physics''': true / false}
 
* {"'''physics''': true / false}
 
Usually animations are processed in the Update() loop. Setting physics to true will instead process the animation in the FixedUpdate() function. This is necessary if you want to animate a rigidbody that interacts with physics.
 
Usually animations are processed in the Update() loop. Setting physics to true will instead process the animation in the FixedUpdate() function. This is necessary if you want to animate a rigidbody that interacts with physics.
  
==== Callback ====
+
=== Callback ===
  
 
* {"'''callback''': ''callback, callable or delegate''}
 
* {"'''callback''': ''callback, callable or delegate''}
 
Setting the callback option to a function (callable, delegate or what you want to call it) will not directly apply the animated value to the object but instead pass it to the callback function. This can be used together with RigidbodyMover and RigidbodyRotator to pass the position/rotation of a rigidbody through MovePosition() and MovePosition() to improve interaction of the rigidbody with other objects.
 
Setting the callback option to a function (callable, delegate or what you want to call it) will not directly apply the animated value to the object but instead pass it to the callback function. This can be used together with RigidbodyMover and RigidbodyRotator to pass the position/rotation of a rigidbody through MovePosition() and MovePosition() to improve interaction of the rigidbody with other objects.
  
=== Easing Classes ===
+
== Easing Classes ==
  
AniMate bundles following easing classes. You can roll your own by extending the AnimationEasing interfaces.
+
AniMate bundles following easing classes. You can roll your own by extending the AnimationEasing interface.
  
 
* LinearEasing
 
* LinearEasing
Line 118: Line 140:
 
<javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "easing": QuarticEasing}};</javascript>
 
<javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "easing": QuarticEasing}};</javascript>
  
=== Animation Drive Classes ===
+
== Animation Drive Classes ==
  
 
An animation drive calculates the current value of an animated object. The default is RegularDrive and calculates the current value like (startValue + currentTime * change).
 
An animation drive calculates the current value of an animated object. The default is RegularDrive and calculates the current value like (startValue + currentTime * change).
Line 129: Line 151:
 
<javascript>Ani.Mate.To(transform, 3, {"rotation": Quaternion.Euler(0,0,180), "drive": SlerpDrive}};</javascript>
 
<javascript>Ani.Mate.To(transform, 3, {"rotation": Quaternion.Euler(0,0,180), "drive": SlerpDrive}};</javascript>
  
=== Special Cases ===
+
== Special Cases ==
  
==== Animating Rigidbodies ====
+
=== Animating Rigidbodies ===
  
 
To correctly animate rigidbodies, the animation has to be done in FixedUpdate and the position/rotation applied through MovePosition and MoveRotation. This can be done with AniMate using the "physics" (to use FixedUpdate) and "callback" (to call the Move* methods).
 
To correctly animate rigidbodies, the animation has to be done in FixedUpdate and the position/rotation applied through MovePosition and MoveRotation. This can be done with AniMate using the "physics" (to use FixedUpdate) and "callback" (to call the Move* methods).
  
<javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "physics": true, "callback": rigidbody.MovePosition}};</javascript>
+
<javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "physics": true, "callback": rigidbody.MovePosition});</javascript>
 +
 
 +
There's also a shorthand to make this call simpler:
 +
 
 +
<javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "rigidbody": rigidbody});</javascript>
  
For this to work corretly, isKinematic should be enabled on the rigidbody.
+
For this to work correctly, isKinematic should be enabled on the rigidbody.
  
==== Animating Rotation ====
+
=== Animating Rotation ===
  
 
There are two possible ways to animate rotation. Either by using '''By''' and eulerAngles to create a rotation without target or by using rotation and SlerpDrive to create a rotation from two positions that will rotate with as little torque as possible.
 
There are two possible ways to animate rotation. Either by using '''By''' and eulerAngles to create a rotation without target or by using rotation and SlerpDrive to create a rotation from two positions that will rotate with as little torque as possible.

Revision as of 21:06, 4 July 2008

Author: Adrian

Contents

Description

Complex animations are best done using Unity's animation system and bones. But there are times when you just need to move a block or during rapid prototyping when you don't want to bother a 3d application. Unity provides some functions for those cases like Lerp, Repeat, PingPong etc. They are fine and can be very powerful using coroutines. However, inspired by TweenLite library from Flash, I wanted a simpler approach that would allow me to dispatch an animation and not have to bother with it further, while also making my code a bit simpler.

Features

  • Animate any field/property that supports +/-/* mathematical operations.
  • Apply easing to animation to create more pleasing transitions.
  • Chain / delay animations.
  • Animate rigidbodies
  • Animate rotation

Installation

To install AniMate, simply import the Ani.boo script into your Standard Assets/Scripts folder. AniMate is then available from any script outside of the Standard Assets folder and from any Boo script inside it.

Download

Download AniMate here: [1]

Version History

v1 (4. July 2008)

  • Initial release

Quick Introduction

Animations are created with To, From and By. All three methods take three arguments:

  1. The object you want to animate properties on (this for current object)
  2. The duration of the animation in seconds
  3. A Hash containing the properties to be animated as keys and the target as value. The Hash can also contain AniMate options.

For example, to move a game object to another position: <javascript>Ani.Mate.To(gameObject.transform, 2, {"position": new Vector3(0,20,0)});</javascript> It's also possible to animate multiple properties with one call: <javascript>Ani.Mate.To(gameObject.transform, 2, {"position": new Vector3(0,20,0), "localScale": new Vector3(2,2,2)});</javascript>

Methods

AniMate is available from any script using Ani.Mate. It provides following methods:

To

  • To(object :Object, duration :float, properties/options :Hashtable) :WaitForSeconds(duration)
  • To(object :Object, duration :float, properties :Hashtable, options :Hashtable) :WaitForSeconds(duration)

Create an animation from the current value to the given value.

<javascript>// Move object from it's current position to (10,0,0) Ani.Mate.To(transform, 3, {"position": new Vector3(10,0,0)});</javascript>

From

  • From(object :Object, duration :float, properties/options :Hashtable) :WaitForSeconds(duration)
  • From(object :Object, duration :float, properties :Hashtable, options :Hashtable) :WaitForSeconds(duration)

Create an animation from the given value to the current value.

<javascript>// Move object from (0,0,0) to it's current position Ani.Mate.From(transform, 3, {"localScale": new Vector3(0,0,0)});</javascript>

By

  • By(object :Object, duration :float, properties/options :Hashtable) :WaitForSeconds(duration)
  • By(object :Object, duration :float, properties :Hashtable, options :Hashtable) :WaitForSeconds(duration)

Create an animation from the current value and animate by the given value.

<javascript>// Move object by 10 units on the x axis Ani.Mate.By(transform, 3, {"localScale": new Vector3(10,0,0)});</javascript>

Stop

  • Stop(object :Object, name :string)

Stop animations on an object for a given field/proeprty.

<javascript>Ani.Mate.Stop(transform, "position");</javascript>

StopAll

  • StopAll(object :Object)

Stop all animations on a given object.

<javascript>Ani.Mate.StopAll(transform);</javascript>

Options

The hash that is given to To, From and By can contain AniMate options. Options can also be passed to those methods as separate hash for applying the same options to multiple animations or to avoid name conflicts.

Easing

  • {"easing": AnimationEasing class}

Easing allows to make an animation non-linear. LinearEasing is the default and means that the change will be constant over the whole animation. Different easings can be chosen to, for example, make the animation start fast and the slow down when it approaches it's target. See Easing Classes for what different kinds of easing types are available.

Easing Direction

  • {"direction: EasingType.In / EasingType.Out / EasingType.InOut}

Easing also has a direction that's either In, Out or InOut. In generally means the animation starts fast and then slows down, Out means the animation starts slow and and ends fast and InOut means the animation starts slow, is fastest at halfway and then slows down again.

Delay

  • {"delay: delay in seconds}

If you don't want an animation to start immediately, you can delay the animation with this option.

Rigidbody

  • {"rigidbody: Rigidbody instance}

Shorthand for animating rigidbodies. This makes sure the rigidbody is animated so that it correctly interacts with other physics objects. This option only applies to "position" and "rotation" properties. This is equivalent to {physics: true, callback: rigidbody.MoveXX}.

Physics

  • {"physics: true / false}

Usually animations are processed in the Update() loop. Setting physics to true will instead process the animation in the FixedUpdate() function. This is necessary if you want to animate a rigidbody that interacts with physics.

Callback

  • {"callback: callback, callable or delegate}

Setting the callback option to a function (callable, delegate or what you want to call it) will not directly apply the animated value to the object but instead pass it to the callback function. This can be used together with RigidbodyMover and RigidbodyRotator to pass the position/rotation of a rigidbody through MovePosition() and MovePosition() to improve interaction of the rigidbody with other objects.

Easing Classes

AniMate bundles following easing classes. You can roll your own by extending the AnimationEasing interface.

  • LinearEasing
  • QuadraticEasing
  • CubicEasing
  • QuarticEasing
  • QuinticEasing
  • SinusoidalEasing
  • ExponentialEasing
  • CircularEasing

You can see a demonstration of those easing functions here: [2]

Default is LinearEasing and any other easing is chosen by adding "easing" to the options: <javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "easing": QuarticEasing}};</javascript>

Animation Drive Classes

An animation drive calculates the current value of an animated object. The default is RegularDrive and calculates the current value like (startValue + currentTime * change).

  • RegularDrive
  • SlerpDrive

SlerpDrive only works with Quaternions and can be used to animate rotation so that the object rotates the shortest distance possible.

<javascript>Ani.Mate.To(transform, 3, {"rotation": Quaternion.Euler(0,0,180), "drive": SlerpDrive}};</javascript>

Special Cases

Animating Rigidbodies

To correctly animate rigidbodies, the animation has to be done in FixedUpdate and the position/rotation applied through MovePosition and MoveRotation. This can be done with AniMate using the "physics" (to use FixedUpdate) and "callback" (to call the Move* methods).

<javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "physics": true, "callback": rigidbody.MovePosition});</javascript>

There's also a shorthand to make this call simpler:

<javascript>Ani.Mate.To(transform, 3, {"position": Vector3(10,10,10), "rigidbody": rigidbody});</javascript>

For this to work correctly, isKinematic should be enabled on the rigidbody.

Animating Rotation

There are two possible ways to animate rotation. Either by using By and eulerAngles to create a rotation without target or by using rotation and SlerpDrive to create a rotation from two positions that will rotate with as little torque as possible.

<javascript>// Make two full circles around the y-axis: Ani.Mate.By(transform, 3, {"eulerAngles": new Vector3(0,720,0)}); // Rotate to a value: Ani.Mate.To(transform, 3, {"rotation": Quaternion.Euler(30,90,20), "drive": SlerpDrive});</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox