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 Murtii · Nov 04, 2015 at 01:48 PM · boundsboxcollider

Check if the bounds of the Cube (Unity Primitive) is within EmptyGameObjectWithBoxCollider bounds

If the user has clicked within the EmptyGameObjectWithBoxCollider bounds a Cube must be added at that location BUT considering the EmptyGameObjectWithBoxCollider (let say of Scale (1, 0.5, 1)) bounds.

If the user clicks too far, let say to the left, of the EmptyGameObjectWithBoxCollider, the newly created Cube Size( let say of Scale (0.5,0.5,0.5)) may go out of the bounds of the EmptyGameObjectWithBoxCollider so the program must automatically rearrange it within the EmptyGameObjectWithBoxCollider bounds.

I am using Raycasting to know the exact location where the user has clicked and can even add a newly created Cube at that location but without Bound checks.

So How can I check the bounds of the Cube as well as EmptyGameObjectWithBoxCollider so that the cube is always placed within it. In the below image the Highlighted in Green is EmptyGameObjectWithBoxCollider. The Cube added has no Bounds checks alt text

With the bounds check the image should look like this alt text

corrected.jpg (66.8 kB)
emptygameobject.jpg (68.0 kB)
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
1

Answer by Fire_Cube · Nov 04, 2015 at 02:45 PM

Assuming the rotation is 0,0,0, and that the collider is a box collider:

you can get the minimum position on x, y and z with something like this :

 Vector3 worldCenter = transform.position + boxCollider.center;
 
 Vector3 minimumXYZ = new Vector3(
         worldCenter.x - boxCollider.size.x/2,
         worldCenter.y - boxCollider.size.y/2,
         worldCenter.z - boxCollider.size.z/2);
 

and the maximum x,y and z with :

 Vector3 worldCenter = transform.position + boxCollider.center;
     
 Vector3 maximumXYZ = new Vector3(
             worldCenter.x + boxCollider.size.x/2,
             worldCenter.y + boxCollider.size.y/2,
             worldCenter.z + boxCollider.size.z/2);

after this, assuming the mesh is a cube of size 1, 1, 1(cube of unity), we can get the boundaries of the cube :

 Vector3 cubeMinimumXYZ = new Vector3(
                 cube.transform.position.x - cube.transform.scale.x/2,
                 cube.transform.position.y - cube.transform.scale.y/2,
                 cube.transform.position.z - cube.transform.scale.z/2);
 //and...
 Vector3 cubeMaximumXYZ = new Vector3(
                 cube.transform.position.x + cube.transform.scale.x/2,
                 cube.transform.position.y + cube.transform.scale.y/2,
                 cube.transform.position.z + cube.transform.scale.z/2);


then, we got all the data we need to check if the cube is in the boundaries :

 //minimum : 
 if(cubeMinimumXYZ.x < minimumXYZ.x)
 {
     cube.transform.position += new Vector3(
             minimumXYZ.x - cubeMinimumXYZ.x, 0, 0);
 }
 if(cubeMinimumXYZ.y < minimumXYZ.y)
 {
     cube.transform.position += new Vector3(0, 
             minimumXYZ.y - cubeMinimumXYZ.y, 0);
 }
 if(cubeMinimumXYZ.z < minimumXYZ.z)
 {
     cube.transform.position += new Vector3(0, 0, 
             minimumXYZ.z - cubeMinimumXYZ.z);
 }
 
 //and the maximum : 
 
 if(cubeMaximumXYZ.x > maximumXYZ.x)
 {
     cube.transform.position += new Vector3(
             maximumXYZ.x - cubeMaximumXYZ.x, 0, 0);
 }
 if(cubeMaximumXYZ.y < maximumXYZ.y)
 {
     cube.transform.position += new Vector3(0, 
             maximumXYZ.y - cubeMaximumXYZ.y, 0);
 }
 if(cubeMaximumXYZ.z < maximumXYZ.z)
 {
     cube.transform.position += new Vector3(0, 0, 
             maximumXYZ.z - cubeMaximumXYZ.z);
 }


I think this should do it :)

good luck

Comment
Add comment · Show 4 · 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 Murtii · Nov 04, 2015 at 05:50 PM 0
Share

Thank you very much. I am extremely grateful that really helped me out. I just used a slight different version from yours. I used the same cube$$anonymous$$inimumXYZ and cube$$anonymous$$aximumXYZ logic to find $$anonymous$$imumXYZ and maximumXYZ respectively. Short Demo :

      Vector3 $$anonymous$$imumXYZ = new Vector3(
                 EmptyGameObjectWithBoxCollider.transform.position.x - boxCollider.size.x / 2,
                 EmptyGameObjectWithBoxCollider.transform.position.y - boxCollider.size.y / 2,
                 EmptyGameObjectWithBoxCollider.transform.position.z - boxCollider.size.z / 2);

and it is taking well care of $$anonymous$$ and max bounds on x axis but what about y axis ? How can I limit the Cube on max y and $$anonymous$$ y . Don't worry about my change made (Short Demo). Using your own logic can you please help out. Please see the picture alt text

avatar image Fire_Cube Murtii · Nov 05, 2015 at 10:01 AM 0
Share

I can't see the image :/ can you upload it again plz?

avatar image Murtii Fire_Cube · Nov 05, 2015 at 01:57 PM 0
Share

alt text

false.png (335.7 kB)
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Get EXACT SkinnedMeshRenderer bounds 2 Answers

Bounds and unwanted offset 0 Answers

Bounds with Rotation: Getting y value of the point where its on the most left side of the collider (bounds.min.x) Note that the box collider has been rotated as well. 2 Answers

Calculating BoxCollider bounds and center in runtime 1 Answer

Dynamically creating a box collider for an object with multiple, rotated, and scaled mesh renderers 0 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