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;
}
}
}
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 :)
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. (?)
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);
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.
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!