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 alaroid · Oct 08, 2014 at 08:19 AM · cube

Looking for CheckCube

I have to check for a free space in a virtual world of cubes (or mashes with a CubeCollider) to initiate a new Object. But what is the most elegant method to check for a free space? Of course CheckCapsule or CheckSphere is exact what I'm looking for, only that they are not for Cubes.

Initiating the rigidbody with a collider and check a collision is not possible, because initiation is not a move, so there is no collision or trigger.

SweepTest is a nice function, but not for a new object.

So, is there something like CheckCube?

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

Answer by robertbu · Oct 08, 2014 at 08:29 AM

No built in function. Assuming you want to check some cubic space for an intersection, you can do it by 'hand'. Generate a bounds for the space you want to check, then cycle through all the game object and check their colider.bounds against the bounds of the test area. A bit of untested code:

 function IntersectBounds(bounds : Bounds, objects : GameObject[]) : boolean {
     for (var go : GameObject in objects) {
         if (bounds.Intersects(go.collider.bounds)) {
             return true;
         }
     }
     return false;
 }

Note if you are going to be doing this a lot or have a large number of cubes, it might pay to build an array or generic List of colliders, and pass than in instead of an array of game objects. Note due to floating point inprecision, you might need to make the bounds test space just a smidgen smaller to avoid false positives.

Comment
Add comment · Show 7 · 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 alaroid · Oct 08, 2014 at 08:32 AM 0
Share

Thanks for your fast answer! That was my next thought, but I hoped that it was not necessary. :)

avatar image alaroid · Oct 13, 2014 at 03:44 PM 0
Share

But it does only work with world-space axis-aligned cubes!

avatar image robertbu · Oct 13, 2014 at 08:43 PM 0
Share

Well, it only check a space defined by a bounds which is a rectangular, 3D, axes aligned space, but the object being placed does not have to be a cube.

avatar image alaroid · Oct 13, 2014 at 09:06 PM 0
Share

But if the cube is not aligned, the testing bounds are much too large.

avatar image robertbu · Oct 13, 2014 at 09:46 PM 0
Share

I'm unclear about the nature of the issue. Whether you are trying to place a rotated cube, or if you are trying to see if a rotated code can be axes aligned and then placed. If the latter, you can use mesh.bounds and then scale the result by Transform.localScale, or you can set the rotation to Quaternion.identity before reading the renderer.bounds, restoring the rotation if necessary.

Show more comments

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

Rolling Cube with Rigidbody? 5 Answers

Random Genration of Cube 2 Answers

Finding the dimensions of a cube, exploiting them. 2 Answers

instantiate works on one but not other object 0 Answers

How to add vertices to a cube 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