• 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 ac$$anonymous$$eve t$$anonymous$$s 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 t$$anonymous$$s 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

· Add your 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 w$$anonymous$$ch 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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Raycast Rotate Help 1 Answer

Why I can't play animations in my 2D game? 0 Answers

Cant set animation to legacy 4 Answers

Use RenderTexture to sample pixels from camera? 2 Answers

When I try to mark an animation as legacy, the debug menu is faded out 0 Answers


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