void attacking()
{
if ((_attack.attackk || Input.GetMouseButtonDown(0)) && spr.flipX==false)
{
anim.Play("SlashR");
}if((_attack.attackk || Input.GetMouseButtonDown(0)) && spr.flipX==true){
anim.Play("SlashL");
}
if (health.rectTransform.sizeDelta.x <= 0)
{
anim.SetBool("death", true);
isDeath = true;
}
else
{
anim.SetBool("death", false);
isDeath = false;
}
}
İ'm trying to make a 2D hack&slash game,and that's why i need a combat system. So,to do this i wanted to make two animation,SlashR is for right sword and SlashL is for left sword.What i want to do is that if i press mouse button and my character is turn Right then will play SlashR anim,and vice versa. But when i press mouse button(flipX=false) SlashR animation plays only for one times.After one animation is finished,animation stops completely and vice versa. But there's weird thing as well,when i turn my character left and after that turn right,SlashR animation is playing again. Note: Loop time unchecked for SlashR and SlashL
Based on your State-diagram from your Animator, your SlashR
and SlashL
state does not go back to Idle
after they are done.
This will cause the slashing-states to stay in their current state after the animation is done.
Make a state-transition from SlashR
and SlashL
to Idle
(or whatever state you want) and it should fix the problem.
But there's weird thing as well,when i turn my character left and after that turn right,SlashR animation is playing again.
That is because in your
if ((_attack.attackk || Input.GetMouseButtonDown(0)) && spr.flipX==false)
{
anim.Play("SlashR");
}if((_attack.attackk || Input.GetMouseButtonDown(0)) && spr.flipX==true){
anim.Play("SlashL");
}
you do not check if the player is currently attacking first.
This results in the other animation to immediately start playing when the player moves left-right while it is attacking.
The solution is to properly make use of your animators as so:
And your new code should be:
if ((_attack.attackk || Input.GetMouseButtonDown(0)) && spr.flipX==false) {
anim.SetTrigger("SlashR");
} else if((_attack.attackk || Input.GetMouseButtonDown(0)) && spr.flipX==true){
anim.SetTrigger("SlashL");
}