• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by unity_v8WpbMHs_-Td2Q · Mar 10, 2018 at 06:08 PM · animationmovement2d gametopdownkeypress

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);
         }
         
     }

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_v8WpbMHs_-Td2Q · Mar 10, 2018 at 06:32 PM 0
Share

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.

avatar image meat5000 ♦ unity_v8WpbMHs_-Td2Q · Mar 10, 2018 at 06:42 PM 1
Share

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.isKinematic = true; }
 else rigidbody.isKinematic = 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 determine to animation state and whilst the attack STATE is in progress IsMoving is false

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

232 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to modify a movement step by step to continuous movement? 0 Answers

how can i trigger a animation while moving in a 2d topdown game,How can i trigger movement animations in a 2d topdown game 0 Answers

Adding small acceleration to 2d top down movement game? 0 Answers

Detect direction of enemy with MoveTowards 1 Answer

How to create an Animated 2D Line to help the user aim and shoot? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges