When I play my game on the editor or when I build the PC version, all the animations play at the right time, however on Android, the Jump animation is not always played. Basically I have two bool parameters: “Running” and “Jump”, when Running is set to false and Jump is set to true, the Jump animation is played, upon collision with the ground Running is set to true and Jump is set to false, because under these conditions the Run animation is played.
The original code was like this:
public void Jump(){
Body.velocity = new Vector2(0,0);
Body.AddForce(new Vector2(0,360));
Anim.SetBool("Running",false);
Anim.SetBool("Jump",true);
Jumping = true;
}
void OnCollisionEnter2D(Collision2D co){
if(Jumping){
Jumping = false;
Anim.SetBool("Jump",false);
Anim.SetBool("Running",true);
}
}
But while testing in the editor, I realized sometimes (about 1 out of each 100 times) the Jump animation didn’t play, it was hard to track what happened inside the Animator Controller, but with the aid of the editor tools I found out that some times, for some unknown reason the “Jump” parameter remained true after collision with the ground… So I modified the Jump function like this:
public void Jump(){
if(Anim.GetBool("Jump")){
Anim.SetBool("Jump",false);
Anim.Play("PlayerRun",-1,0);
return;
}
Body.velocity = new Vector2(0,0);
Body.AddForce(new Vector2(0,360));
Anim.SetBool("Running",false);
Anim.SetBool("Jump",true);
Jumping = true;
}
I also added some debug code and ran extensive tests in order to find out if these changes really solved the problem, and they did. I’ve been running the game for several days and the problem hasn’t showed up again in both the editor and the PC build… however when I run the game in Android the character does jump, but the Jump animation not always plays, about 1 out of every 20 or 30 times the animation simply doesn’t play. In order to see what was happening with the parameters, I added some debug code, but only to confirm that when the problem pops up, both parameters have the right values… so I don’t know what’s going on.
I can’t tell if this is a Unity’s bug but if it isn’t, then what am I doing wrong?
by the way, I’m working with Unity 5.1.3