Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by unity_mMT9VnZeKExLTQ · Apr 22, 2020 at 03:18 AM · scripting problemscripting beginnermovement scriptattacking

Stop movement while attacking

Hi, I just started Unity and have no experience in programming. Just dived into it like an idiot. Anyway, I would appreciate any help on this. I would like for my character to attack when I press a certain button ( keyboard "C" or xbox360 "A"). When I press the key, I want the character to stop moving (if it is moving) and run the animation, wait until the animation stop, then allow the character to move again. I place an animation event at the end of the animation to trigger the PlayerAttackEnds() function.


The animation runs fine until the end but I can't seem to stop the character movement while it is attacking. How can I do it? And which is better for me to try to do?


A. Add into the attack function someway for it to wait the duration of the animation then continue or


B. Use the animation event to trigger a function to end it (what I'm using right now)

     void Update()
     {
     float moveVertical = Input.GetAxis("Vertical");
     float moveHorizontal = Input.GetAxis("Horizontal");
     movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
     movement = Vector3.ClampMagnitude(movement, 1);
     }

     public void PlayerMovementUsingRigidbody(Vector3 movement)...
  
     public void PlayerAttack()
     {
         Debug.Log(anim.GetBool("attack"));
         if (Input.GetKey(KeyCode.C) == true | Input.GetButton("Fire1") == true) 
         {
             anim.SetBool("attack", true);
             if (anim.GetBool("attack") == true)
                 {
                     movement = Vector3.zero;
                 }
         }
         /*else
         {
             //anim.SetBool("attack", false);
             
         }
         */
     } 
 
     public void PlayerAttackEnds()
     {
         anim.SetBool("attack", false);
     }








Comment
Add comment
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
1

Answer by agior312 · Apr 22, 2020 at 10:57 PM

Hi

Try to help you, I'm a beginner as you are :)

Have you tried to set another boolean for transition between attack and move animations? Something like "moving" and set it to false while attacking then true again while attack is ended. Also, if there is an input for moving I suggest to place a "canMove" bool in the script

 if(Input......)
 { 
      if (canMove) 
      {
           //movement
           anim.SetBool("moving",true);
      }
 }


 if (Input.GetKey(KeyCode.C) == true | Input.GetButton("Fire1") == true)   //==true not necessary
          {
              anim.SetBool("attack", true);

              if (anim.GetBool("attack") == true)   //==true not necessary
                  {
                      anim.SetBool("moving", false);
                      canMove=false;
                  }
                else
                 { 
                       canMove=true;
                  }
          }

Comment
Add comment · Show 1 · 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_mMT9VnZeKExLTQ · Apr 26, 2020 at 07:54 AM 0
Share

sorry for the late reply thanks for the note on the operators made my code much cleaner and nicer to look at

yeah I actually solved it already I used

if (moveDir != Vector3.zero && anim.GetBool("attack") != true)

then player can move

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.

Follow this Question

Answers Answers and Comments

233 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 avatar image

Related Questions

How do I tie my movement script to only selected objects?,How do I tie my movement script to the my selection script? 1 Answer

What should i write that when i press "Shift" it will speed up idk how shift is named? Here's the code: 1 Answer

How to move the object to where the object is already pointing to? 1 Answer

Unity 2D movement and rotate sprite to the direction it is moving in 1 Answer

Auto-move on grid and draw line problem 0 Answers


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