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 /
avatar image
0
Question by SinisterMephisto · Jul 21, 2011 at 03:48 PM · animationimportanimationcurvesplittangent

How do I modify a keyframe/curve's tangent in code?

Hi, I recently noticed that when i split animations Unity modifies the tangents of some of the keys (usually the first, last and/or second to the last) creating a very noticable pop especially in looped animations.

 using UnityEngine; 
 using UnityEditor; 
 using System; 
 using System.Collections.Generic;
 

 public class FixAnimation : ScriptableObject 
 { 
     [MenuItem("Animation/Fix Animation")] 
     static void ListChildTransforms() 
     { 

     GameObject g = Selection.activeGameObject;
     Debug.Log(g.name.ToString());

     if (g.animation && g.animation.clip)
     {
         List<string> clips = new List<string>();

         foreach (AnimationState state in g.animation)
         {
             clips.Add(state.name);
         }

         foreach(string clipName in clips)
         {
             FixClip(g.animation.GetClip(clipName));
         }
     }

      }

      public static void FixClip(AnimationClip clip)
      {

     AnimationClipCurveData[] curveData = AnimationUtility.GetAllCurves(clip,true);
     AnimationCurve anim = null;


     for (int i = 0; i < curveData.Length; i++)
     {

         anim = curveData[i].curve;

         anim.SmoothTangents(0,0);
         anim.SmoothTangents(anim.length-2,0);
         anim.SmoothTangents(anim.length-1,0);

         /*

         anim.keys[0].outTangent = 0;
         anim.keys[0].inTangent = 0;
         anim.keys[length-2].outTangent = 0;
         anim.keys[length-2].inTangent = 0;
         anim.keys[length-1].outTangent = 0;
         anim.keys[length-1].inTangent = 0;
         */

         //replace existing curve
         clip.SetCurve(curveData[i].path, curveData[i].type,curveData[i].propertyName,anim);
      }
      }

 }


I attempted to fix this by code but i am not having much success. It seems when i succed in cleaning up the last frame the first frame suffers. Is there an ordering that need to be followed? what does keyfram.tangentMode control? When editing the animation in the animation view all i need to do is right click on the key and set both tangents to linear and the popping is gone. How do i replicate this in code?

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
0

Answer by Bunny83 · Jul 21, 2011 at 11:32 PM

Have you read this:

http://answers.unity3d.com/questions/48590/how-can-i-convert-animationcurveinouttangent-to-an.html

If you want to use Mathf.Tan keep in mind that it works with radians. If you want to use degrees you have to multiply by Mathf.Deg2Rad

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 SinisterMephisto · Jul 23, 2011 at 06:08 PM 0
Share

I recently tied calculating the linear tangent myself

 float o =0,a =0;
 anim.keys[0].inTangent = 1;
 o = anim.keys[anim.length-1].value - anim.keys[anim.length-2].value;
 a = anim.keys[1].time;// always 1 frame
 anim.keys[anim.length-2].outTangent = (float) $$anonymous$$ath.Atan2(o,a);
 anim.keys[anim.length-1].inTangent = (float) $$anonymous$$ath.Atan2(a,o);

Still no luck

avatar image
0

Answer by SinisterMephisto · Jul 22, 2011 at 11:36 AM

Thanks for the reply. But this makes me wonder how do you specify both tangents as linear? Cos it seems it would be some what relative to the last key. maybe making a triangle with the value of previous key current key and the time between them to calculate the outTangent of the last key and the inTangent of the current key.

It seems the curve editor has the functions i need there has to be a way to reuse that function.

Thanks

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can I make animations snap to a frame? 1 Answer

Animation not importing into Unity with the same curves as Maya 5 Answers

How can I set my Animation Curve's Tangents to Flat in script? 0 Answers

Why can't I edit animation curves imported from Maya? 1 Answer

Unity Animation - Loop around a closed loop trajectory 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