Search Unity

animationClip.SampleAnimation() does not result in visible changes

Discussion in 'Scripting' started by MiaoBolverk, Sep 25, 2018.

  1. MiaoBolverk

    MiaoBolverk

    Joined:
    Feb 6, 2018
    Posts:
    26
    Here is the relevant code snippet:

    Code (csharp):
    1. public class AnimatedGameObject : MonoBehaviour
    2.     {
    3.         private float extentPlayed;
    4.                
    5.         public AnimationClip Clip;
    6.         public GameObject AnimationTarget;
    7.        
    8.         private void Update()
    9.         {
    10.             if (this.extentPlayed >= 1f)
    11.             {
    12.                 return;
    13.             }
    14.  
    15.             Debug.Log(this.extentPlayed);
    16.  
    17.             this.Clip.SampleAnimation(this.AnimationTarget, extentPlayed * this.Clip.length);
    18.             this.extentPlayed += 0.005f;
    19.         }
    20.     }    
    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.

    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?
     
  2. Kayamon

    Kayamon

    Joined:
    Sep 5, 2018
    Posts:
    5
    If the Animator is still active, it's probably overwriting the transforms you've set up. Make sure to either disable it, or perhaps try putting your code in LateUpdate instead of Update, so that SampleAnimation happens after the Animator's had its turn.
     
  3. luosiri

    luosiri

    Joined:
    Dec 19, 2013
    Posts:
    11
    Change the model's "Animation Type" to [Legacy]