Billy is a boy living in a world with many challenges, after 10 years of the "big war" the Morlocks want to unleash again Orion, the greate master of Morlocks. Billy needs rescue all ancient powers to win this epic battle.

Post news Report 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

Your comment will be anonymous unless you join the community. Or sign in with your social account: