Changing GameObject's animation speed in the inspector.

This seems like a common topic, but I couldn’t find any exact solutions to this problem in the forums.

I was hoping to be able to change the speed of a GameObject’s animation in the inspector.

I was thinking that creating a public float to hold a speed value which was multiplied by the Time.deltaTime time that the animation was playing at, might be a way to do it.

If anyone can see what I was trying to do in the following, and point me in a better direction, thanks for your time.

using UnityEngine;
using System.Collections;

public class AnimationSpeedChange : MonoBehaviour {


    public GameObject Person;
    public float MoveSpeed = 1.0f;


    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {
        Person.GetComponent<Animation>().Play("Walk" * MoveSpeed * Time.deltaTime);

    }
}

This can not work. In the Play() Call you multiply a string with numbers, which would result in an error.

You probably want to look at https://docs.unity3d.com/ScriptReference/Animation.html. The example provided sets the animation speed:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour 
{
[INDENT]public Animation anim;
void Start() 
{
[INDENT]anim = GetComponent<Animation>();
foreach (AnimationState state in anim)
{
[INDENT]state.speed = 0.5F;[/INDENT]
}[/INDENT]
}[/INDENT]
}

Furthermore: AnimationState can be used to change the layer of an animation, modify playback speed, and for direct control over blending and mixing.”

So I guess AnimationState is the way to go, check out the Unity documentation how to use it.

1 Like

Thanks for taking the time to help me.

Thanks for helping me.