- Home /
Animation not playing all the times on Android.
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
Answer by Kakaku · Sep 16, 2015 at 03:37 PM
OnCollisionEnter2D it means you are working in 2d mode let me Ask few question to understand it properly
did you just upgrade your unity to unity 5.1.3 from lower version? or you just start with fresh Unity 5.1.3 Installment?
for jump instead of
Body.Velocity = new Vector2(0,0);
Body.AddForce(new Vector2(0,360));
Try to use
var jupSpeed:float = 15.0;
rigidbody2D.velocity.y = jupSpeed;
onGround = false;
function OnCollisionEnter2D(down :Collision2D)
{ onGround = true; }
I upgraded from version 5.1.2... the physics and the collisions work all right, there's no problem with that at all. I'm using OnCollisionEnter2D to trigger the run animation and that works just fine. So, the jump process itself works just fine, the problem is in the animator, the jump animation doesn't play all the times... it does play, but not always... so there are times in which, when the character jumps it keeps moving as if it was walking on solid ground... but it is on middle air! so the jump doesn't look realistic... that's all the problem. It is an animation problem! not a physics problem nor a collision problem. Like I said this is an Android exclusive problem.