Reputation: 76
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
Upvotes: 1
Views: 1182
Reputation: 4056
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");
}
Upvotes: 1
Reputation: 350
Please examine the image.
You can right click the state and select make transition etc.
Use the transitions tool in the Animator state machine. You have to make transition between slashR to idle and idle to slashR and same thing for slashL.
For example: if you want to change state you can set the boolean true like this,
anim.SetBool ("SlashL", true);
Do not forget to hasExit time box.
When exit time is ticked,if the animation is completed then the next animation is played. If Has Exit Time is checked, this value represents the exact time at which the transition can take effect
Upvotes: 1