Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Wenon · Apr 27, 2018 at 12:05 PM · colliderfindontriggerstay

Find all objects inside box collider

Hello, how to make table with all objects inside box collider which is trigger, alternatively find the nearest one? I tried something like this:

   private void OnTriggerStay(Collider[] col)

but Collider[] does not work with OnTriggerStay

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by hexagonius · Apr 27, 2018 at 04:40 PM

If you want to know that at a certain point in time, just use an OverlapBox:
https://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html

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 Wenon · Apr 27, 2018 at 07:31 PM 0
Share

I need object with box collider because rotation and scale and position of collider are dynamic in my project and I need to see this box does it work as it should

avatar image
3

Answer by Harinezumi · Apr 27, 2018 at 12:31 PM

This is not necessarily the best solution, but the simplest that comes to my mind: when a Collider enters a trigger that has this script, it is added to a list, when it exits, it is removed from the list. You can get the current list by calling GetColliders() (just don't modify the contents):

 public class ColliderContainer : MonoBehaviour {
 
     private List<Collider> colliders = new List<Collider>();
     public List<Collider> GetColliders () { return colliders; }
 
     private void OnTriggerEnter (Collider other) {
         if (!colliders.Contains(other)) { colliders.Add(other); }
     }
 
     private void OnTriggerExit (Collider other) {
         colliders.Remove(other);
     }
 
 }
Comment
Add comment · 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 TreyH · Apr 27, 2018 at 04:20 PM 0
Share

@Wenon did this work for you?

avatar image Wenon TreyH · Apr 27, 2018 at 07:28 PM 0
Share

Yes, it works, I found this earlier but I thought there is simpler way

avatar image Chrism24747 · Sep 27, 2020 at 11:54 PM 0
Share

It worked thank you so much

avatar image zosichd · Aug 06, 2021 at 01:37 PM 0
Share

but what if this collider was turned off all this time, and when I turn it on, I already need this list?

avatar image rage_co zosichd · Aug 06, 2021 at 01:56 PM 0
Share

OnTriggerStay is what you're looking for....just simply do this...

 void OnTriggerStay(Collider other)
 {
   if(!colliders.Contains(other))
   {
     colliders.Add(other);
   }
 }

this will also check if the object is already in list so there shouldn't be duplication problems either....hope this helps

avatar image
3

Answer by timothygao8710 · Mar 02, 2021 at 04:16 AM

Adding on the Harinezumi, a HashSet can be used rather than a list, which would yield a lower time complexity(make your code run faster). While the .Remove() and .Contains() method for List runs in O(N^2) and O(N) respectively, the .Remove() and .Contains() for HashSet are both O(1). This can make a huge difference if you're dealing with many objects. Also the code is simpler:

 public class ColliderContainer : MonoBehaviour {
  
      private HashSet<Collider> colliders = new HashSet<Collider>();
 
      public HashSet<Collider> GetColliders () { return colliders; }
  
      private void OnTriggerEnter (Collider other) {
          colliders.Add(other); //hashset automatically handles duplicates
      }
  
      private void OnTriggerExit (Collider other) {
          colliders.Remove(other);
      }
  
  }
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
avatar image
0

Answer by hydrix · Feb 12 at 08:24 PM

First one tells whether Vector3 point resides within the BoxCollider.

Second one gives all Collider[] within the BoxCollider using a layer mask.

 ///<summary>
 ///returns true if the point is within our BoxCollider
 ///</summary>
 public static bool IsWithinBoxBounds(Vector3 point, BoxCollider boxCollider) {
      point = boxCollider.transform.InverseTransformPoint( point ) - boxCollider.center;
      
      float halfX = (boxCollider.size.x * 0.5f);
      float halfY = (boxCollider.size.y * 0.5f);
      float halfZ = (boxCollider.size.z * 0.5f);
      if( point.x < halfX && point.x > -halfX && 
         point.y < halfY && point.y > -halfY && 
         point.z < halfZ && point.z > -halfZ )
          return true;
      else
          return false;
 } //end func IsWithinBoxBounds

 ///<summary>
 ///returns all colliders within BoxCollider
 ///</summary>
 public static Collider[] BoxColliderOverlaps(BoxCollider target, int layerMask, bool hitTriggers) {

     return Physics.OverlapBox(target.transform.position + target.center, target.size * 0.5f, target.transform.rotation, layerMask, hitTriggers ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore);
 
 } //end func BoxColliderOverlaps
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

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

106 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

Related Questions

OnTriggerStay Question 0 Answers

Audio not playing after OnTriggerStay is applied to the scripts (Driving Simulator Project) 0 Answers

Ask for trigger-collision inside Update() 1 Answer

How can I get my script to recognise when a collider exits while the box collider of the trigger object is disabled? 1 Answer

OnTrigger event when colliders are already touching eachother 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