• 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 AsAnAsterisk · Jun 26, 2017 at 06:15 AM · animationscripting problemmovementaienemy ai

How To Stop Enemy Movement During Its Attack Animation

I have an enemy and when it's doing its attack animation I would like it to stop moving and rotating until the animation finishes. I'm sure it involves setting the force to 0 and whatnot but I'm unsure how to phrase the coding in a way that achieves this effect

 public class Chase : MonoBehaviour {
 
     public Transform player;
     public CapsuleCollider cc;
     private Animator anim;
     private Rigidbody myRigidbody;
     private Vector3 moveDirection;
     public Collider attack;
     public Slider healthbar;
     
     void OnTriggerEnter(Collider attack)
     {
       if (attack.gameObject.tag == "PlayerAttack") {
      
           healthbar.value -= 20;
           anim.SetBool("IsInjured", true);
          }
          
       
      }
 
     // Use this for initialization
     void Start()
     {
         anim = GetComponent<Animator>();
         myRigidbody = GetComponent<Rigidbody>();
         
         cc.enabled=false;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         moveDirection = Vector3.zero;
         
 
         if (Vector3.Distance(player.position, this.transform.position) < 4)
         {
             Vector3 direction = player.position - this.transform.position;
             direction.y = 0;
 
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                         Quaternion.LookRotation(direction), 0.3f);
 
             anim.SetBool("IsIdle", false);
             if (direction.magnitude > .8)
             {
                 moveDirection = direction.normalized;
                 anim.SetBool("IsMoving", true);
                 anim.SetBool("IsBiting", false);
                 anim.SetBool("IsInjured", false);
             }
             else
             {
                 anim.SetBool("IsBiting", true);
                 anim.SetBool("IsMoving", false);
             }
 
 
         }
         else
         {
             anim.SetBool("IsIdle", true);
             anim.SetBool("IsMoving", false);
             anim.SetBool("IsBiting", false);
         }
         
          if (healthbar.value <= 0){
             anim.SetBool("IsVulnerable", true);
             }
          else
          {
             anim.SetBool("IsVulnerable", false);
          }
 
         
     }
     public void ActiveCollider(int active){
     
         if (active == 0)
             cc.enabled = false;
         if (active == 1)
             cc.enabled = true;
             }
     public void VulnerableRegain(int active){
     
         
         if (active == 1)
             healthbar.value += 60;
             }        
     
     
     void FixedUpdate()
     {
         myRigidbody.AddForce(moveDirection * 70f);
     }
     
 }

   
   
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

  • Sort: 
avatar image
Best Answer

Answer by AsAnAsterisk · Jun 26, 2017 at 11:30 PM

I solved this myself, actually, I just put this at the end of my code

 public void StopMovement(int active) {
         
         if (active == 1)
             transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
         if (active == 0)
             transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
             
             }

And then put animation events with the function "StopMovement" on all the animations, i tried only doing it with the attack animations but it ended up completely freezing the enemy, so I needed to put StopMovement int=1 animation event at the start of the rest of the animations so it would still move normally.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

231 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

Related Questions

How to make enemy detect a character in his certain range? 1 Answer

Trouble making jumping spider enemies 0 Answers

Animation/Movement Error 1 Answer

Ai sprite animation 8 directional movement 0 Answers

Meeting Animation parameters with an AI sprite. 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