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
3
Question by PEOWIfjpwoiqjf · Jul 19, 2012 at 08:35 PM · vertexworldspacebox colliderlocalspace

Box Collider Vertexes in World Space

How do I get the vertexes of a box collider in world space? (with correct rotation and scale).

Comment
Add comment · Show 1
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 AlucardJay · Jul 19, 2012 at 10:37 PM 0
Share

I'm not sure about the collider, var the$$anonymous$$eshCollider = theCube.GetComponent($$anonymous$$eshCollider).shared$$anonymous$$esh; throws an error for a box collider. But for the mesh try this little mesh reader I just wrote =]

 #pragma strict

 public var theCube : Transform;

 private var theVerts : String = "";
 private var theUVs : String = "";
 private var theNorms : String = "";
 private var theTris : String = "";

 function Start () {
     var the$$anonymous$$esh : $$anonymous$$esh = theCube.GetComponent($$anonymous$$eshFilter).mesh as $$anonymous$$esh;
 
     theVerts = theVerts + "(" + the$$anonymous$$esh.vertices.length.ToString() + ") :";
     theUVs = theUVs + "(" + the$$anonymous$$esh.uv.length.ToString() + ") :";
     theNorms = theNorms + "(" + the$$anonymous$$esh.normals.length.ToString() + ") :";
     theTris = theTris + "(" + the$$anonymous$$esh.triangles.length.ToString() + ") :";
 
     for (var i:int = 0; i < the$$anonymous$$esh.vertices.length; i ++) {
         theVerts = theVerts + "  " + i + "=" + the$$anonymous$$esh.vertices[i].ToString();
         theUVs = theUVs + "  " + i + "=" + the$$anonymous$$esh.uv[i].ToString();
         theNorms = theNorms + "  " + i + "=" + the$$anonymous$$esh.normals[i].ToString();
     }
 
     for (i = 0; i < the$$anonymous$$esh.triangles.length; i ++) {
         theTris = theTris + "  " + i + "=" + the$$anonymous$$esh.triangles[i].ToString();
     }
 }

 function OnGUI () {
     GUI.Label( Rect( 10, 10, Screen.width - 20, 60), "Verts " + theVerts );
     GUI.Label( Rect( 10, 70, Screen.width - 20, 60), "UVs " + theUVs );
     GUI.Label( Rect( 10, 130, Screen.width - 20, 60), "Normals " + theNorms );
     GUI.Label( Rect( 10, 190, Screen.width - 20, 60), "Tris " + theTris );
 }

Can any of the pro's help with a hint/nudge? =]

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by ScroodgeM · Jul 20, 2012 at 06:41 PM

using UnityEngine;
using System.Collections;
public class BoxColliderVerticesLogger : MonoBehaviour
{
    //object with box collider must be assigned here
    public BoxCollider BoxColliderObject;
    private Transform[] markers;
    void Start()
    {
        //creating 8 spheres to mark vertices
        markers = new Transform[8];
        for (int i = 0; i < 8; i++)
        {
            markers[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
        }
    }
    void Update()
    {
        if (BoxColliderObject)
        {
            //get local collider center
            Vector3 boxColliderCenter = BoxColliderObject.center;
            //get local collider extents
            Vector3 boxColliderExtents = BoxColliderObject.extents;
            for (int i = 0; i < 8; i++)
            {
                //get one of vertice offset from center
                Vector3 ext = boxColliderExtents;
                ext.Scale(new Vector3((i & 1) == 0 ? 1 : -1, (i & 2) == 0 ? 1 : -1, (i & 4) == 0 ? 1 : -1));
                //calc local vertice position
                Vector3 vertPositionLocal = boxColliderCenter + ext;
                //move sphere to global vertice position
                markers[i].position = BoxColliderObject.transform.TransformPoint(vertPositionLocal);
            }
        }
    }
}
Comment
Add comment · Show 3 · 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 freefalll · Jan 09, 2013 at 12:21 PM 0
Share

Just wanted to say thanks for this! Tried some other methods but this is working absolutely perfectly. Thanks :)

avatar image TheCatProblem · Jun 12, 2013 at 01:33 AM 0
Share

It's quite handy that BoxColliders store their size and extents in local coordinates. I was trying to get the positions of the collider vertices using the values from the Collider.bounds bounding box and perfor$$anonymous$$g a number of transformations, but this method is clearly superior.

avatar image Din0m1te · Dec 14, 2016 at 05:51 PM 0
Share

Thank you very much! I've tried several ways to achieve this. While all of them worked when the object with the collider had no rotation, this one works in every situation!

avatar image
1

Answer by ScroodgeM · Jul 20, 2012 at 10:30 AM

primitive colliders (sphere, capsule, box, etc) are calculated by unique very cheap way and it can have no any vertices at all

for example, to detect collision between two spheres you just need to (not a code! just for understand method!)

currentDistanceSquared = (collider1.position - collider2.position).sqrMagnitude;
collisionDistanceSquared = Mathf.sqr(collider1.radius + collider2.radius);
if (collisionDistanceSquared < distanceSquared) { [COLLISION HAPPENED] }

so vertices you can get only from mesh collider

for box collider anyway you can get "vertices" by calculating them. all you need is found them in local space of transform that have collider and then translate it to world space.

Comment
Add comment · Show 3 · 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 PEOWIfjpwoiqjf · Jul 20, 2012 at 01:20 PM 0
Share

I need to know the vertices of the box collider, how do I calculate them? Just doing local -> world on the extents does not work.

avatar image PEOWIfjpwoiqjf · Jul 20, 2012 at 01:22 PM 0
Share

I need to get the vertices of the box collider. When I try to do local-world on them it does not work. It does not properly account for scale.

avatar image ScroodgeM · Jul 20, 2012 at 06:43 PM 0
Share

for future post your code example, try to complete your task yourself, don't wait somebody else do it for you. 8)

take a script - it creates (and dynamically updates) 8 spheres in vertices of boxCollider

avatar image
0

Answer by dpolyakov · Mar 07, 2016 at 11:11 AM

for Unity5 you just need to replace

 Vector3 boxColliderExtents = BoxColliderObject.extents;

with this:

 Vector3 boxColliderExtents = b.size / 2.0f;

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 petrucio · Dec 10, 2017 at 09:18 AM 0
Share

That's only valid if your scales are all 1 in the hierarchy. AND aligned to world axes. Sorry but this answer is just wrong on many levels.

avatar image Bunny83 petrucio · Dec 10, 2017 at 12:12 PM 0
Share

No, it's not wrong. It's just an extension of @Scroodge$$anonymous$$'s answer. The scale, offset and rotation is applied afterwards by "TransformPoint". He just posted this answer (which probably should be a comment) to inform people that the "extents" property of the BoxCollider component has been marked as "obsolete" so it shouldn't be used anymore as it might get removed in the future. The extents property is actually declared like this:

 [Obsolete("use BoxCollider.size ins$$anonymous$$d.")]
 public Vector3 extents
 {
     get { return this.size * 0.5f; }
     set { this.size = value * 2f; }
 }

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

12 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

Related Questions

Moving forward in world space with rotation. 1 Answer

rigidbody.MoveRotation neither world space or local? How does it work? 1 Answer

What is the difference between TransformDirection, TransformVector and TransformPoint since they all accept and return a Vector3 value. 1 Answer

About World space and Local space 4 Answers

Raycasting, How to draw a line to the position of the rays end when nothing is hit 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