• 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 /
This question was closed Mar 03, 2020 at 10:00 PM by tormentoarmagedoom for the following reason:

Too subjective and argumentative

avatar image
0
Question by ThePeefRimgar · Mar 03, 2020 at 06:39 PM · physics

Player only colliding & attacking with last spawned NPC, not current collision

So my issue is when the player attacks any object in the Enemy Layer with the Enemy Tag, it will properly do damage and knockback the enemy collided with...but when the player attacks an object in the Enemy Layer with the NPC Tag, it will only ever attack the last spawned NPC, not the one collided with. Im not sure how to go about fixing this or making the collision smoother. ExampleVideo

Here is the Attack function...it is called when the player pushes the attack button. Let me know if you need any other code for context!

  void Attack()
     {
         #region Attack and Attack Combo
         if (!attacking && attackCombo == 1 && comboAttack == false && falling == false)
         {
             attacking = true;
             knockbackDamage = Random.Range(250, 300);
             knockupDamage = Random.Range(200, 250);
             attackTime = attackCooldown;
             animator.SetBool("isAttacking", true);
             animator.SetInteger("attackCombo", 0);
             Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
             foreach (Collider2D enemy in enemiesToDamage)
             {
                 objCollided = enemy.gameObject;
                 if (enemy.gameObject.CompareTag("Enemy"))
                 {
                     if(enemy.gameObject.GetComponent<EnemyBehaviour>().dead == false)
                     {
                         canHitEnemy = true;
                         attackSwipe01.SetActive(true);
                         objCollided.GetComponent<EnemyBehaviour>().TakeDamage(attackDamage);
                         objCollided.GetComponent<EnemyBehaviour>().KnockBack(knockbackDamage, knockupDamage * 2);
                         Debug.Log("PLAYER hit " + enemy.name + " AND DID " + attackDamage + " damage!" + " Enemy has " + objCollided.GetComponent<EnemyBehaviour>().currentHealth + " health remaining!");
                     }
                     if(enemy.gameObject.GetComponent<EnemyBehaviour>().dead == true && canHitEnemy)
                     {
                         PlayerData.PlayerXP += 1;
                         canHitEnemy = false;
                     }
                 }
                 if(enemy.gameObject.CompareTag("NPC"))
                 {
                     objCollided.GetComponent<NPC_Control>().KnockBack(knockbackDamage, knockupDamage * 2);
                     Debug.Log("Player Hit " + enemy.name);
                 }
                 
             }
             
         }


Comment
Add comment · 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 tormentoarmagedoom · Mar 03, 2020 at 07:02 PM 0
Share

is attackPos.position really where you think it is? Are you sure "whatIsEnemies" layer is set correctly?

1 Reply

  • Sort: 
avatar image
0

Answer by ThePeefRimgar · Mar 03, 2020 at 08:48 PM

Solved it! I accidentally set the rb on the NPC controller script to static, so it was only affecting one object.

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

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

201 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

Related Questions

2D 360 degress platformer example needed 0 Answers

Every time my character hits or touches the walls he gets stuck or acts as if the object the character hit was a magnet. 0 Answers

Golfball sphere collider "Hops" over vertex-joined planes at intersection? 1 Answer

ignore forces except gravity? 1 Answer

How to make a system that will let elements interact with other elements? 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