0
\$\begingroup\$

I have this GameObject with an AnimatorController. The AnimatorController has several states in which I want to parametrize the animation's speed because configuration. For this, I've created an animation parameter, called "RotationMultiplier" and set up the "Speed/Multiplier" value to this paramter on the needed states.

Now, my idea is being able to configure this speed from a script "somewhere." I've read a bit about StateMachineBehaviours, but my initial idea was executing this once, instead of once in each frame/state.

The "OnStateMachineEnter" function does not execute at all, and with the OnEnable I don't have access to the Animator (nor the parameters).

So, any idea?

\$\endgroup\$
0
\$\begingroup\$

If this is just an initial configuration, you can set it from the Start() method of an ordinary MonoBehaviour attached to the GameObject you're configuring.

Here's an example that lets you define the parameters in data, and tune them live in the Inspector while the game is playing.

public class AnimationConfiguration : MonoBehaviour {

    [System.Serializable]
    public struct FloatParameter {
         public string name;
         public float value;
    }

    public FloatParameter[] floatParameters;

    void Start() {
        var animator = GetComponent<Animator>();

        foreach(var parameter in floatParameters)
            animator.SetFloat(parameter.name, parameter.value);
    }

    // Allow live editing in the Inspector during play mode.
    void OnValidate() {
        if(Application.isPlaying)
            Start();
    }
}
\$\endgroup\$
  • \$\begingroup\$ There isn't any way to configure this without relaying on an attached GameObject? My idea was to have all this "starter values" somewhere else due code organization and such. \$\endgroup\$ – Neuromante Sep 18 '18 at 22:08
  • \$\begingroup\$ I may have misunderstood your application then. If you want one initial value that affects every object using this Controller, then you can just write the values you want directly into the controller. You only need a script if you want to override that initial value for specific objects / runtime \$\endgroup\$ – DMGregory Sep 18 '18 at 22:11
  • \$\begingroup\$ Yeah, my (asbtract) idea was to have a single file for some parameters (both on animations and other stuff) I wanted to change easily from my code (or a file on application load), so I could avoid both manually changing the controller parameter value and going through all the GameObjects to initialize the same value. \$\endgroup\$ – Neuromante Sep 19 '18 at 8:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.