Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by timuther · Mar 07, 2016 at 04:29 PM · animationanimationcurveanimation clip

Altering animation curves via script

Hi, I'm trying to write an editor extension that alters an animation curve via script. Unfortunately, any attempt I make to silently alter the value of the animation clip fails silently.

I first tried using curve.AddKey but this fails to change the value but that failed to change it (no error, just no change), so I tried altering the keyframe directly. Stepping over the line ' curve.keys[i].value = value;', I can see in the debug watch that 'curve.keys[i].value' never changes - it's as if the 'set' property method is implemented but does nothing.

   void FindOrAddKey(AnimationCurve curve, float time, float value)
     {
         for (int i = 0; i < curve.keys.Length; ++i)
         {
             if (curve.keys[i].time == time)
             {
                 curve.keys[i].value = value;                
                 return;
             }
         }
         curve.AddKey(time, value);
     }

My question is if this is intentional or if it's a bug in Unity? Also, is there another way to edit the curve (I can't find a way to remove a curve from an animation either - there's no opposite of 'AnimationUtility.SetEditorCurve' as far as I can see.

Thanks for your time.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by timuther · Mar 07, 2016 at 04:12 PM

got it - you have to remove the key and re-add, like this: static void FindOrAddKey(AnimationCurve curve, float time, float value) { for (int i = 0; i < curve.keys.Length; ++i) { if (curve.keys[i].time == time) {
curve.RemoveKey(i);
break; } }
int key = curve.AddKey(time, value); }

Comment
Add comment · 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
3

Answer by jaredDumpling · Apr 24, 2019 at 03:25 AM

For anyone else looking, there is a simpler method of modifying key values without having to remove/add.

 Keyframe[] keyframes = animationCurve.keys;
 
 keyframes[0].value = 0;
 keyframes[1].value = 1;
 keyframes[2].value = 0;
 
 animationCurve.keys = keyframes;
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Prevent keyframe interpolation in imported model animation in Unity 1 Answer

Can't add animated properties to a 3D character in 2018 0 Answers

Animation Rigging works differently for different animations 0 Answers

Timeline character animation cancels out the character script gravity logic 1 Answer

Can't attach animationClip to animation 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