Skip to content
Miguel González Viñé edited this page Sep 3, 2015 · 2 revisions

AutoMotion

Created by ChevyRay on September 26th, 2013 at UnityPatterns

Features

  • Position, rotation, and scale tweening
  • Support for both linear and curved motion
  • Several built-in ease types as well as AnimationCurve support
  • Loop and wave values, vectors, and quaternions

How to Use

Tween Coroutines

To perform a basic tween, call MoveTo(), RotateTo(), or ScaleTo() on the transform you want to tween. The second parameter (0.5f), is how long the tween should take.

StartCoroutine(transform.MoveTo(targetPosition, 0.5f));
StartCoroutine(transform.RotateTo(targetRotation, 0.5f));
StartCoroutine(transform.ScaleTo(targetRotation, 0.5f));

Alternatively, to tween from the position instead of to it (reverse tween), you can do this:

StartCoroutine(transform.MoveFrom(targetPosition, 0.5f));
StartCoroutine(transform.RotateFrom(targetRotation, 0.5f));
StartCoroutine(transform.ScaleFrom(targetRotation, 0.5f));

Eased Motion

You can alternatively pass in another parameter, and easing function, to modify how the tween moves. These are located in the Ease class.

StartCoroutine(transform.MoveFrom(targetPosition, 0.5f, Ease.ElasticOut));

If you want to select your ease type from the inspector, you can also use the EaseType enum.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    public float duration;
    public Vector3 targetPosition;
    public EaseType easeType;
 
    void Start()
    {
        StartCoroutine(transform.MoveFrom(targetPosition, duration, easeType));
    }
}

Animation Curves

You can easily create custom curves using Unity’s built-in AnimationCurve class and passing in its Evaluate() method as the ease function to a tween.

using UnityEngine;
using System.Collections;
 
public class Example : MonoBehaviour
{
    public float duration;
    public Vector3 targetPosition;
    public AnimationCurve ease;
 
    void Start()
    {
        StartCoroutine(transform.MoveFrom(targetPosition, duration, ease.Evaluate));
    }
}

Waved/Looped Motion

You can create waved and looped motion using the Auto.Wave() and Auto.Loop() functions as well, which do not require coroutines and can instead be called every frame. Try either of the two lines in this example and note the difference:

void Update()
{
    transform.localScale = Auto.Loop(1.0f, Vector3.zero, Vector3.one);
    transform.localScale = Auto.Wave(1.0f, Vector3.zero, Vector3.one);
}

Sequences & Pausing

Because tweens are driven by coroutines, they are ideal for creating animation sequences. When you are building a sequence coroutine, if you want the sequence to do nothing for a period of time, you can run the Auto.Wait() coroutine. Here is an example of an object, given an array of points, that will move between them (in a loop).

using UnityEngine;
using System.Collections;
 
public class Example : MonoBehaviour
{
    public Vector3[] path;
    public float moveTime;
    public float pauseTime;
    public float turnTime;
    public EaseType moveEase;
    public EaseType turnEase;
 
    void Start()
    {
        StartCoroutine(MoveOnPath());
    }
 
    IEnumerator MoveOnPath()
    {
        //Snap to the first position
        transform.localPosition = path[0];
        transform.LookAt(path[1]);
        int pathIndex = 1;
 
        while (true)
        {
            //Move to the next point
            yield return StartCoroutine(transform.MoveTo(pathIndex[pathIndex], moveTime, moveEase));
 
            //Pause for a moment
            yield return StartCoroutine(Auto.Wait(pauseTime));
 
            //Find the next point
            pathIndex++;
            if (pathIndex == path.Length)
                pathIndex = 0;
 
            //Turn towards the next point
            var targetRotation = Quaternion.LookRotation(path[pathIndex] - transform.position);
            yield return StartCoroutine(transform.RotateTo(targetRotation, turnTime, turnEase));
        }
    }
}