0
\$\begingroup\$

Here is the relevant code snippet:

public class AnimatedGameObject : MonoBehaviour
{
    private float extentPlayed;

    public AnimationClip Clip;
    public GameObject AnimationTarget;

    private void Update()
    {
        if (this.extentPlayed >= 1f)
        {
            return;
        } 

        Debug.Log(this.extentPlayed);

        this.Clip.SampleAnimation(this.AnimationTarget, extentPlayed * this.Clip.length);
        this.extentPlayed += 0.005f;
    }
}

In the Unity Editor, the game object assigned to AnimationTarget contains the animator component controlled by the controller that uses Clip.

However, when I start the game, even though I can see that this.extentPlayed is being incremented every frame, no animation is taking place that I can see.

I am aware that what I am currently trying to do in the Update() method can be achieved by calling SetBool(string name, bool value) on the animator, but the purpose of writing the Update() method this way is to gain familiarity with how the SampleAnimation() method works, so that I can perform more complicated manipulations when necessary.

What changes should I make for the animation to take place?

\$\endgroup\$
1
\$\begingroup\$

Just leaving this in case it is relevant to someone:

You can't use SampleAnimation for sampling a sub-object of an animation. You have to sample the whole object (the parent object).

For example: If you got a character waving animation, you can't sample just the hand. You need to sample the whole character.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

I recently had a problem where a GameObject would not animate via SampleAnimation() in the player. To solve it, I:

  1. made sure an Animator referencing an Animation Controller was attached to the GameObject and

  2. disabled the Animator component.

| improve this answer | |
\$\endgroup\$

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.