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;
		}
    	}
    }

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);

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!