Animate Anything with Mecanim

Checked with version: 4.5

-

Difficulty: Beginner

In this session we will have an introductory look at animating objects with the Mecanim animation tool and state machine. Many people feel that Mecanim is meant exclusively for animating humanoid bi-ped characters, but it can be used for so much more - and not only character animation, but animating UI Elements, opening doors, changing lights... any publicly exposed value can be animated with Mecanim. Let's do it! Tutor: Adam Buckner

Animate Anything with Mecanim

Beginner Animation

DoorActivator

Code snippet

using UnityEngine;
using System.Collections;

public class DoorActivator : MonoBehaviour {

    public Animator[] lights; 

    private Animator animator;

    void Awake () {
        animator = GetComponent ();
    }

    void OnTriggerEnter (Collider other) {
        if (other.gameObject.tag == "Player") {
            animator.SetBool ("Open", true);
            foreach (var light in lights) {
                light.SetTrigger ("Activate");
            }
        }
    }

    void OnTriggerExit (Collider other) {
        if (other.gameObject.tag == "Player") {
            animator.SetBool ("Open", false);
        }
    }
}