• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Professor Snake · Apr 23, 2016 at 09:41 AM · legacyanimationsample

Legacy animation state sampling and reverting

Hello there,

I'm using the legacy animation system with some procedural elements for a 2.5D shooter. I'm calculating the player's torso aiming rotation based on the weapon's current forward, but i need it to not be affected by animations like firing. I figured i'd achieve this by saving the specifics on all animation states in the animation component, disable them all save for the aiming one, sample the weapon's current forward and then revert back to the way the states were. For some reason this does not work and at the end of the frame i'm left with just the aiming animation playing.

What am i doing wrong?

 var animStates:List.<AnimationState>;
    function LateUpdate(){
        SampleStates();
        animationObject.animation["Aim"].enabled = true;
        animationObject.animation["Aim"].weight = 1;
        animationObject.animation.Sample();
        currentForward=weapon.transform.forward;
        RevertStates();
     }
     function SampleStates(){
         for (var state : AnimationState in animationObject.animation){
             animStates.Add(state);
         }
         for (state in animationObject.animation){
 //iterate a second time just in case changing a state affects other states immediately
             state.enabled=false;
             state.weight=0;
         }
     }
     function RevertStates(){
         for(var state:AnimationState in animStates){
             animationObject.animation[state.name].enabled=state.enabled;
             animationObject.animation[state.name].weight=state.weight;
             animationObject.animation[state.name].time=state.time;
         }
             animStates.Clear();
     }



Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Apr 23, 2016 at 11:57 AM

Well, AnimationState is a class and therefore a reference type. When you store them in array you don't really save their state but a reference to the instances. AnimationState is actually derived from "TrackedReference" and the managed object itself doesn't have any fields, only properties which call methods in native code. If you want to save the actual state you would have to copy all values in a custom class. Here's an example in C# (I usually don't use UnityScript and realised too late that you do ^^)

 // C#
 public class AnimStateSave
 {
     public string name;
     public bool enabled;
     public float weight;
     public float time;
     public float speed;
     public int layer;
     public AnimationBlendMode blendMode;
     public AnimStateSave(){}
     public AnimStateSave(AnimationState state)
     {
         CopyFrom(state);
     }
     public void CopyFrom(AnimationState state)
     {
         name = state.name;
         enabled = state.enabled;
         weight = state.weight;
         time = state.time;
         speed = state.speed;
         layer = state.layer;
         blendMode = state.blendMode;
     }
     public void ApplyTo(AnimationState state)
     {
         // state.name = name;
         state.enabled = enabled;
         state.weight = weight;
         state.time = time;
         state.speed = speed;
         state.layer = layer;
         state.blendMode = blendMode;
     }
 }


So instead of storing "AnimationState"s in your List you would store "AnimStateSave"

     animStates.Add(new AnimStateSave(state));

To assign them back you can simply do:

     state.ApplyTo( animationObject.animation[state.name] );
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Professor Snake · Apr 24, 2016 at 02:59 AM 0
Share

Well that was silly. Thanks!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is Samples in 2D Animation and how to change animation speed correctly? 2 Answers

Viseme animation using mecanim? 0 Answers

Play(offsetInSamples) acuracy - still cannot get it to work perfectly 5 Answers

Where is my sample option in animations? 2 Answers

Audio Source Pitch effect Legacy Animation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges