1
\$\begingroup\$

I'd like to change the property speed of an animation from the animator. For example, I'd like the player to be able to reload faster with upgrades. So I have a basic reload animation on the gun animator, and I would like to change its speed depending on the reload speed level the player has. But I can't find how to change this property in C#.

Here is what I have so far :

void Start() {
    gunAnim = gun.GetComponent<Animator> ();
}

void Update() {
    if (Input.GetKeyDown (KeyCode.R)) {
        gunAnim.Play("reload");
        reloadSound.Play();
    }
}
\$\endgroup\$
  • \$\begingroup\$ docs.unity3d.com/ScriptReference/Animator.html look at the Functions and you will see SetFloat, SetBool etc etc... That is what you will be using \$\endgroup\$ – Savlon Dec 13 '14 at 13:36
  • \$\begingroup\$ This is to set Animator variables. I could use it if I could set a Float to the time of an animation. Is it possible? \$\endgroup\$ – Elfayer Dec 13 '14 at 13:43
1
\$\begingroup\$

You can use Blend Trees to change animation speed.

First of all you should add float parameter to Character Controller. Then create Blend Tree and double click on it. It'll open Blend Tree editor. Select Blend Type 1D and select created float parameter. Add 5 same animations of reloading. Each animation in Blend Tree has two parameters: Threshold and Speed. Set speed of animations in accordance on your skill. For example: 1.0, 1.25, 1.5, 1.75, 2.0. Threshold is a value of float parameter when animation will have preffered speed. You can set it in accordance on skill level number (1, 2, 3, 4, 5) or animation speed (1.0, 1.25, 1.5, 1.75, 2.0). When you want to change animation speed in script you should use method SetFloat with name of parameter and new value.

Be careful if you set value between two thresholds you can get unexpected result. There are no linear interpolation between two thresholds. For example if you have 2 animations with threshold (1, 2) and speed (1, 2) and set parameter to 1.5 animation will have speed 1.3.

\$\endgroup\$
  • \$\begingroup\$ Excelent ! A bit complexe to read your block, but line by line, it works perfectly, thanks ! ;) \$\endgroup\$ – Elfayer Dec 14 '14 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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