Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

same Animator different speed values

Discussion in 'Scripting' started by Sparking02, Jul 13, 2017.

  1. Sparking02

    Sparking02

    Joined:
    Sep 10, 2015
    Posts:
    25
    Hi, I am developing a little tool for the artist, they are developing some static obstacles like moving dangerous objects, that do the same movement over and over, but they want to change the speed of the clip using a slider, I already have that they pick a gameobject with a animator put it on the script and I take the list of animations and the current speed, and after they move the slider the speed actually changes but when they use the same animator on differents gameobject the last one to start is the speed that rules all of the animations, is there a way to change the speed of a clip, just in the gameobject?

    this is how I set the speed

    for(int i = 0 ; i < animations.runtimeAnimatorController.animationClips.Length ; i++)
    {
    controller = (AnimatorController)animations.runtimeAnimatorController;
    controller.layers[0].stateMachine.states.state.speed = animSpeed;
    print(controller.layers[0].stateMachine.states.state.name + " have speed of " + animSpeed);
    }


    I hope you can help me, and In advance thanks and sorry for my bad english
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    AnimatorControllers and the AnimatorStateInfo.speed property are shared across all instances that use said controller. so if you set the speed value for a state in mecanim to 0.5, all instances that use that animation will use the 0.5 animation speed.

    Instead try using the Animator.Speed property or write up a StateMachineBehaviour which will figure out which speed to use. you can have the speeds saved in a Monobehaviour which the StateMachineBehaviour can grab via Animator.GetComponent<T>.
     
  3. Sparking02

    Sparking02

    Joined:
    Sep 10, 2015
    Posts:
    25
    Ok, StateMachineBehaviour? thx