How do I get the vertexes of a box collider in world space? (with correct rotation and scale).
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 *= 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*.position = BoxColliderObject.transform.TransformPoint(vertPositionLocal);*_ _*}*_ _*}*_ _*}*_ _*}*_ _**_
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.
for Unity5 you just need to replace
Vector3 boxColliderExtents = BoxColliderObject.extents;
with this:
Vector3 boxColliderExtents = b.size / 2.0f;