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 Fobwashed · Jan 07, 2015 at 11:44 AM · collisionenemyontriggerenter2d

Checking 2DCollisions During a single Update

I have a weapon that launches a round and damage should only be dealt to enemies within it's Collider2D when it impacts the ground. What's the best way to deal with this? What I have now is as follows.

 public virtual void Spawn()
     {
         gameObject.SetActive(true);
 
         PlayerControls.Instance.PlaySound(audioFire);
 
         isHitGround = false;
         GetComponent<Collider2D>().enabled = false;
     }
 
 //called when the weapon hits the ground
 protected virtual void HitGround()
     {
         if (!isHitGround)
         {
             isHitGround = true;
             isSetToExpire = true;
             GetComponent<Collider2D>().enabled = true;
 
             renderer.enabled = false;
             PlaySoundOnGround(audioHit);
         }
     }
 
 //And in the update
 protected override void Update () 
     {
         if (isSetToExpire)
         {
             isSetToExpire = false;
             gameObject.SetActive(false);
             return;
         }
 
         if(height <= 0)
         { HitGround(); }
     }
 
 //It should only check the OnTriggerEnter2D when the Collider2D is enabled.
 protected virtual void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.CompareTag("Enemy"))
         {
             if (col.gameObject.GetComponent<EnemyBase>().isAlive)
             { col.gameObject.GetComponent<EnemyBase>().HitByBullet(damage); }
         }
     }

On Spawning the weapon, the Collider2D.enabled is set to false so that it doesn't check collisions while in the air. When it hits the ground, the Collider is enabled so that it can check OnTriggerEnter2D before going through it's update again and disabling itself to return to the available weapon pool.

For the most part, this seems to work but it randomly doesn't for one out of say 10 shots.

My theory on why this is that it's due to both the weapons AND enemies being recycled from a pool. So when the weapon's Collider2D is disabled, it retains the collisions it had up to that point and those collisions haven't exited. So if the same bullet's Collider2D turns on upon impact and encounters the same recycled enemy.Collider2D, it counts it as a OnTriggerStay2D rather than an OnTriggerEntry2D. If this is the case, does anyone know how to cleanse the weapon's Collider2D on spawn?

Alternatively, is there a better way to do what I'm trying to do here? Maybe I'm overcomplicating things.

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Mario-Like Enemy Death System Not Working 1 Answer

OnTriggerEnter2D Not Working 1 Answer

Trying to spawn enemies 1 Answer

Enemy not taking damage on collisions. 2 Answers

Enemy collision with wall, destroys wall 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