Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
0
Question by Marky5959 · May 11, 2020 at 09:56 AM · transformboxcolliderworldspacelocalpositionlocalrotation

How can I get an OverlapBox with the exact same size and position as a BoxCollider?

I have a box collider on an object with a parent where I can adjust the size and centre along with the rotation and position on the transform of the object the collider is attached to using a script, plus the position of the transform on the parent object, in order to get the box just the right size and position in the world.


I want to get the exact same resulting position and size box but with Physics.OverlapBox. Normally I'd mess about with trial and error but the suggested Gizmos.DrawWireCube only allows for position and scale so I'm struggling to visualise the results.


So OverlapBox allows me to adjust the centre, halfExtents and orientation.


HalfExtents seems self explanatory, just half the size. And I guess I can just copy rotation into orientation? looks like the same thing? But maybe not? Will it need adjusting to account for the change in centre on the box collider?


Centre however is less obvious. With box collider I use both it's centre and the objects transform position. I assume OverlapBox won't be using the transform of the object the script is attached to? So I'll have to adjust centre to be the result of the collider centre combined with that transform. Is there a simple way to do that?


I'm thinking there will be adjustments based on local space to world space. Like the OverlapBox centre is the world position, so I'll need to find out what position the collider.centre is when converted back to world space? Would it be like the child of a child? as the centre seems local to a transform which itself has parent.


But I want to know if I'm even barking up the right tree before I start trying to get my head around that. Thanks in advance for any pointers.

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
2
Best Answer

Answer by Pangamini · May 11, 2020 at 10:11 AM

You can visualize the box by setting Gizmos.matrix to collider.transform.localToWorldMatrix, then just drawing collider's localspace box. After you set the matrix to localToWorldMatrix, you can just draw with center being the local collider's center property and extents (as you described).

For the OverlapBox, you can do pretty much the same, you just have to calculate the center yourself. So, for example (untested)

 Vector3 worldCenter = collider.transform.TransformPoint(collider.center);
 Vector3 worldHalfExtents = collider.transform.TransformVector(collider.size * 0.5f); // only necessary when collider is scaled by non-uniform transform
 Physics.OverlapBox(worldCenter, worldHalfExtents, collider.transform.rotation);
Comment
Add comment · Show 2 · 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 Marky5959 · May 11, 2020 at 01:51 PM 0
Share

Thanks, this seems to be working.

If anyone is interested in more info see here for a similar answer https://stackoverflow.com/questions/57982000/how-to-change-the-orientation-for-an-%20overlapbox-and-a-gizmo

Here is what I did, basically copied from that link. It produced a wireframe cube just like the boxcollider.

 void OnDrawGizmos()
     {
         Color prevColor = Gizmos.color;
         $$anonymous$$atrix4x4 prev$$anonymous$$atrix = Gizmos.matrix;
 
         Gizmos.color = Color.red;
         Gizmos.matrix = transform.localToWorld$$anonymous$$atrix;
 
         Vector3 boxPosition = transform.position;
 
         // convert from world position to local position 
         boxPosition = transform.InverseTransformPoint(boxPosition) + collider.centre;
 
         Gizmos.DrawWireCube(boxPosition, collider.size);
 
         // restore previous Gizmos settings
         Gizmos.color = prevColor;
         Gizmos.matrix = prev$$anonymous$$atrix;
     }
avatar image Pangamini Marky5959 · May 11, 2020 at 10:11 PM 0
Share
 Vector3 boxPosition = transform.position;
 transform.InverseTransformPoint(boxPosition);

Doesn't this just return Vector3(0,0,0)?

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

163 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 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

localposition not working? Calculating a vector in front of an object. Picture and code included 1 Answer

Script won't set right rotation? 1 Answer

Random.insideUnitSphere doesn't work as expected 0 Answers

How do you set a local rotation for 1 axis but keep the other axis world space untouched?,How do you set the local rotation of 1 axis without effecting the world rotation of others? 1 Answer

transform.localPosition problem. 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