Post news RSS Controlling speed animation with a prefab

Unity have a problem with management of speed for animations, so...

Posted by on

Recently I need change the speed of an animation in a model in my game.
Well, is something so simple, but I dont found a standard way of to do this in Unity, my option : create a generic script for all models in identify by TAG and name in Unity's enviroment.

using UnityEngine;
using System.Collections;

public class NPCAnimationSetting : MonoBehaviour 
{

    void Awake()
    {
        Animation anim = null;
        GameObject[] items = GameObject.FindGameObjectsWithTag("NPC");
        foreach (GameObject item in items)
        {
            Debug.Log(item.name);
            switch (item.name)
            {
                //conversador...
                case "npc20021":
                    anim = (Animation)item.GetComponent<Animation>();
                    anim["parado"].speed = 0.4f;
                    break;

                //loirinho andarilho...
                case "npc20094":
                    anim = (Animation)item.GetComponent<Animation>();
                    anim["sentado"].speed = 0.3f;
                    break;

                //menina
                case "npc20091":
                    anim = (Animation)item.GetComponent<Animation>();
                    anim["parado"].speed = 0.3f;
                    break;


            }
        }



    }


    void OnDrawGizmos()
    {
        Gizmos.color = Color.magenta;
        Gizmos.DrawSphere(transform.position, 0.02f);
    }
}

This class run every time that a level was loaded and search for all objects with TAG, and change the speed of animation (preserving the speed for all levels).

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.