Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by AngryCSharpNerd · Feb 20, 2013 at 03:09 AM · animationvelocitycrossfadesword

Crossfading happens too fast

Okay, so I have this coding for first person melee where the sword animation plays in the directions the keys the plater presses.

The problem I have, however, is that sometimes the crossfading happens too fast and it almost seems like the sword instantly switched to a new animation rather than move to it. It is because the speed of it depends on the controller's velocity, so I basically need it to where the animation speed will slow down when it is crossfading.

     private var t:float;
     var head:CharacterController;
     private var anispeedthing = .3;
     private var anispeedlimit = 1;
     
 function Start()
     {
     SwordBobble();
 }
     
     function SwordBobble()
     {
         var ani = gameObject.Find("blade").animation;
         while(true)
     {
             print(head.velocity.magnitude/8+" ani, "+head.velocity.magnitude+" speed");
             if(head.isGrounded && !ani.IsPlaying("swordslash") && head.velocity.magnitude < 0.2 )
         {
                 ani["Sword_Idle"].speed = 0.5;
                 ani.CrossFade("Sword_Idle");        
                 yield WaitForSeconds(0.001);
             }
         else{
             ani["Sword_Idle"].speed = 0.1;
             yield;
         }
             
             if(head.isGrounded && !ani.IsPlaying("swordslash") && !Input.GetAxis("Left") && !Input.GetAxis("Right") && Input.GetAxis("Forward") && !Input.GetAxis("Backward"))
         {
                 if(head.velocity.magnitude < 1)
             {
                     ani["SwordBobble"].speed = anispeedthing;
                     ani.CrossFade("SwordBobble");
                 }
                 else if(head.velocity.magnitude > 1)
             {
                     ani["SwordBobble"].speed = head.velocity.magnitude/8;
                     ani.CrossFade("SwordBobble");
                 }        
                 yield WaitForSeconds(0.001);
             }
         else{
             ani["SwordBobble"].speed = 0.3;
             yield;
         }
             
             if(head.isGrounded && !ani.IsPlaying("swordslash") && head.velocity.magnitude > 1 && Input.GetAxis("Left") && !Input.GetAxis("Right"))
         {
                 if(head.velocity.magnitude < 1)
             {
                     ani["SwordBobble_Left"].speed = anispeedthing;
                     ani.CrossFade("SwordBobble_Left");
                 }
                 else if(head.velocity.magnitude > 1)
             {
                     ani["SwordBobble_Left"].speed = head.velocity.magnitude/8;
                     ani.CrossFade("SwordBobble_Left");
                 }            
                 yield WaitForSeconds(0.001);
             }
         else{
             ani["SwordBobble_Left"].speed = 0.3;
             yield;
         }
     
             if(head.isGrounded && !ani.IsPlaying("swordslash") && head.velocity.magnitude > 1 && Input.GetAxis("Right") && !Input.GetAxis("Left"))
         {    
                 if(head.velocity.magnitude < 1)
             {
                     ani["SwordBobble_Right"].speed = anispeedthing;
                     ani.CrossFade("SwordBobble_Right");
                 }
                 else if(head.velocity.magnitude > 1)
             {
                     ani["SwordBobble_Right"].speed = head.velocity.magnitude/8;
                     ani.CrossFade("SwordBobble_Right");
                 }        
                 yield WaitForSeconds(0.001);
             }
         else{
             ani["SwordBobble_Right"].speed = 0.3;
             yield;
         }
             
             
             if(head.isGrounded && !ani.IsPlaying("swordslash") && head.velocity.magnitude > 1 && !Input.GetAxis("Left") && !Input.GetAxis("Right") && Input.GetAxis("Backward") && !Input.GetAxis("Forward"))
         {
                 if(head.velocity.magnitude < 1)
             {
                     ani["SwordBobble_Backward"].speed = anispeedthing;
                     ani.CrossFade("SwordBobble_Backward");
                 }
                 else if(head.velocity.magnitude > 1)
             {
                     ani["SwordBobble_Backward"].speed = head.velocity.magnitude/8;
                     ani.CrossFade("SwordBobble_Backward");
                 }                
                 yield WaitForSeconds(0.001);
             }
         else{
             ani["SwordBobble_Backward"].speed = 0.3;
             yield;
         }
         }
     }
     
 
Comment
Add comment · Show 3
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 Benproductions1 · Feb 20, 2013 at 03:26 AM 0
Share

O$$anonymous$$G SO $$anonymous$$ANY ONE LINERS

sorry, I could help, but this code is unreadable! You have so many things stuffed into 1 line, I'll help you if you fix up your code :)

avatar image AngryCSharpNerd · Feb 20, 2013 at 12:17 PM 0
Share

Lol, sorry, I forgot how sloppy I am. Anyway, all the forward, backward, left and right animations work practically the same way by how the animation speed is deter$$anonymous$$ed, so I need to somehow adjust the speed lower once you're no longer touching the directional key. (?)

avatar image AngryCSharpNerd · Feb 20, 2013 at 12:17 PM 0
Share

The very first is a sword idle animation.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DeBunked · Feb 20, 2013 at 10:50 PM

You can add floating point values to the animation.CrossFade function that determine the timespan over which animations fade out, like: ani.CrossFade("Sword_Idle", 2);

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 AngryCSharpNerd · Feb 23, 2013 at 04:04 AM 0
Share

It seems that for some reason, the crossfading itself is glitched. I turned all of my animations into one simple speed, and yet this jumping stuff still happens.

avatar image
0

Answer by blackshtormx · Sep 08, 2016 at 05:32 PM

ok, first i'd like to say that it's kinda late to answer 3 yr old question, but i think people still are searching this. It's the purpose of my answer. This jumping between animations happens, because transition starts when particular animation that is now playing is ended, and it needs to start again(that is called loop). at this moment transition can't happen between 2 animations, obviously cause animation ended. I also had this problem and i solved it like that:

First. adjust a floating point on crossfade. For eg: ani.CrossFade("SwordBobble_Backward", 0.2f);


Next. make your "SwordBobble_Backward" animation longer, i don't mean make it slower or something. just copy last keyframe and put it 2 secs away.


Lastly some coding:

 ani.CrossFade("SwordBobble_Backward", 0.2f);
                     if (ani["SwordBobble_Backward"].time >= YourPreLastKeyframeTime)
                     {
                         ani["SwordBobble_Backward"].time = 0.0f;
                     }

now let's wrap up everything: We add floating point, next we duplicate animation last keyframe and paste it 2 seconds away(that doesn't matter, just to make sure that it's away from your old last keyframe:) ) then we do coding. that should work!

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

12 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

Related Questions

animator.crossfade() not working properly 2 Answers

Pause Animation.CrossFade 4 Answers

Getting player's local direction 0 Answers

How to make soccer ball spin according to its velocity in 2D top down game 0 Answers

How to i go about playing a animation when Left mouse is clicked? C# 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