0
\$\begingroup\$

I want to change the reload speed of my player when a button is pressed in the pause menu.

I created a button and attached the following script to it (as suggested by the documentation here: https://docs.unity3d.com/ScriptReference/AnimationState-speed.html ):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseUpgrade : MonoBehaviour {

public Animation reloadAnim;
private float startSpeed;
// Use this for initialization
void Start () {
    startSpeed = reloadAnim["Reloading"].speed;
}

public void upgradeSpeed()
{
    reloadAnim["Reloading"].speed += 0.5f;
}

But this does not work. I cannot seem to drag the reload animation into the public variable declared above. Is this the correct way to solve this?

enter image description here

The above image shows my animation controller for my player. I am trying to change the speed of the "reloading" animation clip.

I am trying to drap the animation clip from my projects folder into the public variable below, but I get a cross sign preventing me from dragging it in as if its the wrong type or something.

enter image description here

Thanks

\$\endgroup\$
3
  • \$\begingroup\$ Can you show us what you're trying to drag to where? A screenshot may help. There's common confusion between Animation, Animator, and AnimationClip entities in Unity, each of which refers to something different, so it's easy to get two of them mixed up. \$\endgroup\$
    – DMGregory
    Commented Nov 9, 2018 at 2:57
  • \$\begingroup\$ @DMGregory Ive updated the question with relative screenshots. Thanks \$\endgroup\$ Commented Nov 9, 2018 at 9:41
  • \$\begingroup\$ Ah, I thought it might be that. An AnimationClip (a set of keyframes/curves for animating something) is not the same thing as an Animation, which in Unity is a type of Component that plays AnimationClips (a less powerful version of the Animator component). So what you're trying to do is put a square peg in a round hole. It sounds like the real problem you want to solve is "How do I change the speed of one AnimationClip in my Controller being played by my Animator component?" Is that right? \$\endgroup\$
    – DMGregory
    Commented Nov 9, 2018 at 11:06

1 Answer 1

0
\$\begingroup\$

I managed to solve this. Unity provided a solution for this but is not clear in the documentation. It has been explained in detail here: https://forum.unity.com/threads/mecanim-change-animation-speed-of-specific-animation-or-layers.160395/page-2

To summarize(as provided by gevarre):

Goal: Change the playback speed of a particular Mechanim animation state in script at runtime.

  1. Set up the states in an Animation Controller. (We'll call the animation state we want to change "VariableAnimState".)
  2. In the Animator Controller editor window, select the "Parameters" tab.
  3. Click the "+" sign and create a float parameter.
  4. Name this new float parameter "VariableAnimSpeed".
  5. By default, the value of the float will be 0, so change it to 1 (If you want the animation to start at normal speed...)
  6. Now select the animation state that you want to change so you can see it in the inspector. (In our case, it's VariableAnimState)
  7. In the inspector, notice there is a field labeled "Speed". Leave this at default, which will normally be 1.
  8. Below the Speed field, there is a blank field labeled "Multiplier". Click the checkbox to the right of it labeled "Parameter".
  9. Now back in the Multiplier field, click the dropdown arrow and select the parameter you want to control it. (In our case it's "VariableAnimSpeed")

Note: if you don't create the float parameter first, you will get an error. You have now finished setup.

  1. In script, here are the relevant bits. Change as needed:
public Animator animator;

void ChangeStateSpeed()
{
    animator.SetFloat("VariableAnimSpeed", 2.0f);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .