• 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 JakeStew2 · May 15 at 02:12 AM · 2d gameshootingontriggerenteroncollisionenterbullet-prefab

OnTrigger bullet with multiple colliders on enemy

Hi, My enemies use multiple colliders, one to detect the player and one for detecting when hit with bullets but I cant figure out how to make the bullets pass through the first collider without triggering the damage and destroying the bullets. Thanks.

Bullet Code

public GameObject hitEffect; public int damage = 40;

 void OnTriggerEnter2D(Collider2D collision)
 {
     Enemy enemy = collision.GetComponent<Enemy>();
     if (enemy != null)
     {
         enemy.TakeDamage(damage);
     }


     GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
     Destroy(effect, 0.22f);
     Destroy(gameObject);
 }

Enemy Code

private void OnCollisionStay2D(Collision2D other) { if (other.gameObject.tag == "Player") { if (attackSpeed <= canAttack) { other.gameObject.GetComponent().UpdateHealth(-attackDamage); canAttack = 0f; } else { canAttack += Time.deltaTime; } } }

 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Player")
     {
         target = other.transform;
     }
 }

 private void OnTriggerExit2D(Collider2D other)
 {
     if (other.gameObject.tag != "Player")
     {
         return;
     }
     target = null;
 }


 public void TakeDamage (int damage)
 {
     health -= damage;
     
     if (health <= 0)
     {
         Die();
     }
 }

 void Die ()
 {
     GameObject effect = Instantiate(deathEffect, transform.position, Quaternion.identity);
     Destroy(effect, 0.22f);
     Destroy(gameObject);
 }


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 sharatachary · May 15 at 03:02 AM 0
Share

Could you please share screenshot of the inspector in Unity Editor for the enemy gameobject. Just want more clarity whether enemy has two colliders on it or has nested colliders.

As you are using trigger for collisions the bullet will pass through both colliders. If you have nested colliders then you can check which gameobject did you collide the parent or the child and based on that info you can implement your logic.

1 Reply

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

Answer by sharatachary · May 15 at 02:57 AM

Modify the bullet code:


 void OnTriggerEnter2D(Collider2D collision)
  {
      Enemy enemy = collision.GetComponent<Enemy>();
      if (enemy != null)
      {
          enemy.TakeDamage(damage);
          GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
          Destroy(effect, 0.22f);
          Destroy(gameObject);
      }
  }

So bullet will only be destroyed when it hits an enemy.

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 JakeStew2 · May 15 at 09:49 AM 0
Share

Thankyou very much this worked perfectly, I can't believe the solution was so simple I feel like an idiot. Regardless Thankyou.

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

193 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

Related Questions

OnCollisionEnter2D ignores Z axis!,OnCollissionEnter2D ignores z axis. 0 Answers

Trying to get a player to shoot in Unity 2D. 1 Answer

OnTriggerEnter do not triggers 1 Answer

How do I use an enemy's OnTriggerEnter to collide with stationary player? 2 Answers

OnTriggerEnter() and OnCollisionEnter(), OnControllerColliderHit() not working with character controller 2 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