Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 pojomcbooty · May 17, 2018 at 09:31 PM · collidervector3triggersbounds

best way to check if a Vector3 point is within any of multiple colliders

I've been building a system to lay out building prefabs across a grid at random. there's also trees laid out first, and some other items.

I've got a procedural river and along the river I have multiple box colliders to keep trees and buildings out of the river, as well as colliders in other areas to block spawning of items.

as I intentionally need to animate all the buildings appearing one by one, I am trying to build arrays of valid Vector3 locations from the start() method of my main GameObject, so that I have it all ready before the game starts

In order to make sure nothing overlaps, I only know of two ways to check if a Vector3 is within the bounds of a collider, bounds.contains or oncollisionenter/ontriggerenter

as there are multiple colliders , for efficiency, I'd prefer not to iterate through all colliders and use bounds.contains, but I need to make sure a point is not within any of the colliders.

if possible I'd like to combine all the colliders as one mesh collider but this seems difficult if not impossible, as well as potentially inefficient ?

so the next thing I thought of was to use triggers / isKinematic to check, but that will need to be done while the game is running and physics is in play , so I assume that's not possible in start() ?

so I need some advice , maybe I need to better understand update() ? is there any way to defer some work untili the first few update() runs ?

any help is very much appreciated!

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

1 Reply

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

Answer by pojomcbooty · May 18, 2018 at 11:34 PM

I was looking at this all wrong. I was thinking about start() method like it was awake(), just because some of my code worked one way when attached to an object, and differently in start(). Convinced now it was just bad code.

Anyway, in the end I created a temporary cuboid at the Vector3, and used Physics.OverlapBox (yes even from start method) and it works well. two things to note would be:

  1. exact location of the fake cuboid is very important

  2. layermasking or tag checking is important (ignore terrains etc)

  3. size of the overlapbox is very important , I used the colliders ".extents"

    public static bool SpawnLocationValid(Vector3 potentialLocation){

              GameObject tempObj = new GameObject("temporary cube with collider (SpawnLocationValid)");
                 tempObj.transform.position = potentialLocation + Vector3.up * 1.0f;
                 var bCol = tempObj.AddComponent<BoxCollider>();
                 bCol.isTrigger = true;
             
                 var rb = tempObj.AddComponent<Rigidbody>();
                 rb.isKinematic = true;
                 rb.useGravity = false;
     
                 Collider[] colls = Physics.OverlapBox(potentialLocation, bCol.extents);
     
                 foreach (Collider c in colls){
                     if (c.CompareTag("NoSpawnAnything")){
                         GameObject.Destroy(tempObj);
                         return false;
                     }else{
                         Debug.Log("tag = " + c.tag + ", object = " + c.name);
                         GameObject.Destroy(tempObj);
                     }
                 }
                 GameObject.Destroy(tempObj);
                 return true;
     
                 
             }
    
    
    
    
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 pojomcbooty · May 18, 2018 at 11:37 PM 0
Share

oh for #3 , the colliders extents in this case were a cube of size (1,1,1) but you would probably need larger cubes to check the size of a house, a tree, etc.

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

120 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

Related Questions

Find a Touch in the BoxCollider 1 Answer

Get closest point on collider on at a certain z depth 0 Answers

Unity Height Glitch (explained) 1 Answer

How Does OnTriggerEnter() Work? 0 Answers

How would i find the center of an object with a Y axis offset to find the center on the top face? 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