How can I stop my character from walking during the attack animation?
I am working with Unity in 2D. My character can move around successfully and when not moving, he can also attack. But if he attacks while moving he doesn't stop like I want him to. With this code he stops walking only if I hold down the button for attack. I want him to attack and stop moving until the animation for attack stops and then go back to walking if the movement buttons are held down. I have no clue what to do.
void Update()
{
CheckInput();
}
void CheckInput()
{
IsMoving = false;
IsAttacking = false;
var horiz = Input.GetAxisRaw("Horizontal");
var vert = Input.GetAxisRaw("Vertical");
var attack_input = Input.GetAxisRaw("Attack");
if (horiz < 0 || horiz > 0 || vert < 0 || vert > 0)
{
IsMoving = true;
if (rigidBody.velocity.x != 0 || rigidBody.velocity.y != 0)
{
PrevDirection = rigidBody.velocity;
}
}
if (attack_input > 0)
{
IsAttacking = true;
}
if (IsAttacking == false)
{
forceDelta = new Vector2(horiz, vert);
CalculateMovement(forceDelta * speed);
}
else if (IsAttacking == true)
{
IsMoving = false;
forceDelta = new Vector2(0, 0);
CalculateMovement(forceDelta * speed);
}
}
Answer by meat5000 · Mar 10, 2018 at 06:20 PM
Hmm try adding a deadzone. A float of exactly 0 is hard to achieve so IsMoving might rarely be false; e.g if (horiz < 0.1
~~ etc . GetAxis instead of raw may be better fitting. Also,
if (attack_input > 0)
{
IsAttacking = true;
IsMoving = false;
Debug.Log("Attack State ACTIVATED");
}
and,
else if (IsAttacking == true)
{
//forceDelta = new Vector2(0, 0); <- Maybe you still need this
rigidbody.velocity = Vector2.zero;
Debug.Log("Stopped Rigidbody");
}
Your movement should be based on the animation clip's state of Play if you want to not move upon release but when Attack has finished. For example, access the Animator and determine when the Attack state is in Play or not and make IsMoving depend on that instead of whether or not the button is pressed.
I made those changes and again it seems he only stops moving if I hold down the attack button. Still moves and attacks at the same time if I only tap it.
This means that although your velocity is hitting zero there is some momentum i.e there is another value or vector kicking right back in when you let go. You can also set your speed to 0. You can brute force it in several ways.
1) Whilst IsAttacking is true Brute Force Spam Velocity 0 in FU() with no other conditions.
void FixedUpdate()
{
if(IsAttacking) { rigidbody.velocity = Vector3.zero; }
}
2) Space-glue
if(IsAttacking) { rigidbody.is$$anonymous$$inematic = true; }
else rigidbody.is$$anonymous$$inematic = false;
3) Directional glue
if(IsAttacking) { rigidbody.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY; } //Add any constraints here you need anyway,
else RigidbodyConstraints2D.none; //Or any contraints you need
To address the other issue of holding down the button, you should access the Animator to deter$$anonymous$$e to animation state and whilst the attack STATE is in progress Is$$anonymous$$oving is false