Script: synchronize animation with movement


  • Add a Script object to the project.


  • Open the property dialog for the Script object and paste the script you can find below the video on this page, to the code window of the Script object.


  • This short script adjusts the animation speed based on SkinMesh object's translation speed.


  • Because this 3d model has multiple animation sets, the script will also activate the 'wave' animation when the SkinMesh object is still and the 'walking' cycle when it is moving instead.


  • Note how 3D Rad takes care to smoothly blend between the two animation cycles!


  • << Previous page

    Next page >>

    Please allow some time for the movie above to load. If you can't see the video after a reasonable delay, please click here. Note that this clip has no audio.


    int AnimationSet = 0;
    int AnimationSetNew = 0;
    void Main()
    {
       if (iInitializing())
       {
          AnimationSet = 0;
          AnimationSetNew = AnimationSet;
       }
       else
       {
          if (IN_0 > 0.2)
          {
             AnimationSetNew = 0; //walk
             //set SkinMesh's walk animation speed based on
             //current SkinMesh object's translation speed
             OUT_1 = iFloatInterpolate(IN_0,0.2,50,0,50,true);
          }
          else
          {
                AnimationSetNew = 2; //wave
                OUT_1 = 1;
          }
          //set current animation set for the SkinMesh object,
          //but only if it is different than the one currently
          //playing. We need to avoid resetting the animation
          //at every script loop, for performance reasons.
          if (AnimationSetNew != AnimationSet)
          {
                AnimationSet = AnimationSetNew;
                OUT_2 = AnimationSet;
          }
       }
    }