• 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
Question by Allurance · Mar 22, 2022 at 01:26 AM · 2d2d game2d-platformerenemyattack

Enemy's initial attack doesn't deal damage to player

Good evening, I'm having an issue where any enemy my player first encounters attacks, the first attack doesn't deal damage. However, subsequent attacks after that register the damage. I'm not quite sure why and was wondering if someone could help. Thank you! Here's 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;

 private EnemyPatrol patrol;

 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 - PlayerHealth.instance.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;
             //DealDamage();
             anim.Play("attack" + attackNum);
             timeBtwAttack = startTimeBtwAttack;
         }
         else
         {
             patrol.enabled = true;
         }
     }
     else
     {
         timeBtwAttack -= Time.deltaTime;
     }
     
 }

 private void DealDamage()
 { 
     if (isNear)
     {
         PlayerHealth.instance.TakeDamage(damageAmount);
     }
     if (attackNum == 1)
     {
         damageAmount = attack1;
     }
     else if (attackNum == 2)
     {
         damageAmount = attack2;
     }
     else if (attackNum == 3)
     {
         damageAmount = attack3;
     }
     
     attackNum++;

     if (attackNum > 3)
     {
         attackNum = 1;
     }

 }
 private void OnDrawGizmos()
 {
     Gizmos.color = Color.blue;
     Gizmos.DrawWireCube(attackPos.position, new Vector2(attackRangeX, attackRangeY));
 }
Comment

People who like this

0 Show 1
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 spooneystone · Mar 22, 2022 at 02:15 AM 0
Share

Where is your "attackNum" declared ?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by W4uLi · Mar 22, 2022 at 02:08 AM

@Allurance Your AttackNum is set to 1 at the start but if you call DealDamage() it will be put to 2 because of the attackNum++ that gets called without restriction. Set AttackNum to 0 at the start and when attackNum > 3

You could also clean it up a bit more and swap those ifs with a switch case like:

    private void DealDamage()
     {
         if (isNear)
         {
             switch (attackNum)
             {
                 case 1:
                     damageAmount = attack1;
                     break;
                 case 2:
                     damageAmount = attack2;
                     break;
                 case 3:
                     damageAmount = attack3;
                     attackNum = 0;
                     break;
             }
             PlayerHealth.instance.TakeDamage(damageAmount);
             attackNum++;
         }
     }

Hope that works and I didn't mess up somewhere.

Comment
Allurance

People who like this

1 Show 5 · 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, 2022 at 02:38 AM 0
Share

It's giving me a warning of "Invalid Layer Index '-1' UnityEngine.Animator:Play (string)

avatar image W4uLi Allurance · Mar 22, 2022 at 02:49 AM 0
Share

Check that your animations are named the same as they are in the animator controller. Also maybe you want to put the anim.Play at the bottom of the DealDamage function but before the attacknum++

avatar image Allurance W4uLi · Mar 22, 2022 at 02:57 AM 0
Share

I appreciate your help, I discovered that Unity was trying to play the attack animation before attackNum was incremented so attackNum was still 0 when playing an animation which doesn't exist. I ended up setting attackNum to 1 at the start and checked if before the animations were played which fixed it. Thank you for your help.

Show more comments

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

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

Shade Cloak in Unity 1 Answer

[2d Game]How to attach gameObject with HingeJoints 1 Answer

simulate gravity in a 2D game 3 Answers

How to make the hand follow the weapon that is pointed to the mouse pointer in 2d platformer?,How to make the body parts move with the weapon for unity 2d platformer game ? 0 Answers

2d c# boss follow code 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