Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Allurance · Mar 21 at 05:15 PM · 2d-platformerattacking

How to stop attack animation midway.

In my 2d game I have my enemies attack once the player is in attack range. However, when the player goes out of attack range, the damage is still dealt. I have the damage calculations tied with the attack animations of the enemy so the player is only dealt damage at a specific point of the attack animation. How can I stop the attack animation once the player is out of attack range during the animation?

Here is my code for the enemy attacks:

 private int damageAmount;
 private int attackNum = 1; //attack index
 private Animator anim;
 private bool inAttackRange;
 private bool isNear;
 private float timeBtwAttack;
 private float currentDistance;

 [SerializeField] private float distance = 2f;
 [SerializeField] private float startTimeBtwAttack;
 [SerializeField] private Transform attackPos;
 [SerializeField] private LayerMask whoIsPlayer;
 [SerializeField] private float attackRangeX, attackRangeY;
 [SerializeField] private int attack1, attack2, attack3;
 [SerializeField] private GameObject player;
 private EnemyPatrol patrol;
 private bool isAttacking = false;

 private void Awake()
 {
     anim = GetComponent<Animator>();
     patrol = GetComponent<EnemyPatrol>();
     timeBtwAttack = startTimeBtwAttack;

 }

 private void Update()
 {
     //is player in attack range
     inAttackRange = Physics2D.OverlapBox(attackPos.position, new Vector2(attackRangeX, attackRangeY), 0, whoIsPlayer);

     currentDistance = Mathf.Abs(transform.position.x - player.transform.position.x);

     //check if distance from player is near
     if(currentDistance > distance)
     {
         isNear = false;
     } else
     {
         isNear = true;
     }

     if (timeBtwAttack <= 0)
     {

         if (inAttackRange && isNear)
         {
             patrol.enabled = false;
             anim.Play("attack" + attackNum);
             timeBtwAttack = startTimeBtwAttack;
         }
         else
         {
             patrol.enabled = true;
         }
     }
     else
     {
         timeBtwAttack -= Time.deltaTime;
     }
     
 }

 private void DealDamage()
 {
     
     PlayerHealth.instance.TakeDamage(damageAmount);
     attackNum++;
     if (attackNum == 1)
     {
         damageAmount = attack1;
     }
     else if (attackNum == 2)
     {
         damageAmount = attack2;
     }
     else if (attackNum == 3)
     {
         damageAmount = attack3;
     }
     else if (attackNum > 3)
     {
         attackNum = 1;
     }
 }
Comment
Add comment · Show 2
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 JethRow · Mar 21 at 07:50 PM 0
Share

@Allurance i answered in reply, forgot to tagg u :)

avatar image Okey356 · Mar 22 at 10:22 AM 0
Share

I see pros do it and higher MMR (5k?)people do it too, I have quick attack turned on but if I click 'a' on the same target it just keeps auto attacking but if I click on a different target the animation stops. I've tried the 'h' button and that doesn't work either. Do these people just click on different creeps until the timing is right? Or am I missing something completely?

MyAARPMedicare.com

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JethRow · Mar 21 at 07:49 PM

Hello, Create an Animation event at the point u think it would be ideal to send the damage to the player, at that point create a function that will check if the player is near enough, either with colliders, checksphere, raycast or distance vector u think would be ideal, if player is in range at that point, send dmg, if not then dont :) I hope I helped!

Comment
Add comment · 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 Allurance · Mar 22 at 12:29 AM 0
Share

Thanks so much, it worked!

avatar image JethRow · Mar 22 at 05:20 AM 0
Share

@Allurance I am glad it helped! Good luck!

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

148 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

Related Questions

Why is my character taking damage when attacking & why is it attacking multiple times per hit? 2 Answers

2D Player Movement 0 Answers

Why the Unity 3D Tutorials don't work with Unity 3? 2 Answers

Enemy Movement and Pacing 1 Answer

How do you get your character to look the way you are walking? 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