- Home /
Drawing a bounding box similar to box collider
I'm creating a script which spawns objects inside an area, and I'd like to be able to see the area inside the editor similar to how I can see the bounds on a box collider.
Is there a simple way of doing this? My parameters would be very much the same as the box collider's as well, with Center:{x,y,z} and Size:{x,y,z} adjustable in the inspector.
Answer by robertbu · May 22, 2013 at 10:08 PM
Here is a code solution that shows the mesh bounding box. I've commented out a bit of code you can swap back in that displays the box collider bounds instead. It is tagged as "ExecuteInEditMode()" so you can see the lines while editing.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode()]
public class ShowMeshBounds : MonoBehaviour {
public Color color = Color.green;
private Vector3 v3FrontTopLeft;
private Vector3 v3FrontTopRight;
private Vector3 v3FrontBottomLeft;
private Vector3 v3FrontBottomRight;
private Vector3 v3BackTopLeft;
private Vector3 v3BackTopRight;
private Vector3 v3BackBottomLeft;
private Vector3 v3BackBottomRight;
void Update() {
CalcPositons();
DrawBox();
}
void CalcPositons(){
Bounds bounds = GetComponent<MeshFilter>().mesh.bounds;
//Bounds bounds;
//BoxCollider bc = GetComponent<BoxCollider>();
//if (bc != null)
// bounds = bc.bounds;
//else
//return;
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
v3FrontTopLeft = transform.TransformPoint(v3FrontTopLeft);
v3FrontTopRight = transform.TransformPoint(v3FrontTopRight);
v3FrontBottomLeft = transform.TransformPoint(v3FrontBottomLeft);
v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
v3BackTopLeft = transform.TransformPoint(v3BackTopLeft);
v3BackTopRight = transform.TransformPoint(v3BackTopRight);
v3BackBottomLeft = transform.TransformPoint(v3BackBottomLeft);
v3BackBottomRight = transform.TransformPoint(v3BackBottomRight);
}
void DrawBox() {
//if (Input.GetKey (KeyCode.S)) {
Debug.DrawLine (v3FrontTopLeft, v3FrontTopRight, color);
Debug.DrawLine (v3FrontTopRight, v3FrontBottomRight, color);
Debug.DrawLine (v3FrontBottomRight, v3FrontBottomLeft, color);
Debug.DrawLine (v3FrontBottomLeft, v3FrontTopLeft, color);
Debug.DrawLine (v3BackTopLeft, v3BackTopRight, color);
Debug.DrawLine (v3BackTopRight, v3BackBottomRight, color);
Debug.DrawLine (v3BackBottomRight, v3BackBottomLeft, color);
Debug.DrawLine (v3BackBottomLeft, v3BackTopLeft, color);
Debug.DrawLine (v3FrontTopLeft, v3BackTopLeft, color);
Debug.DrawLine (v3FrontTopRight, v3BackTopRight, color);
Debug.DrawLine (v3FrontBottomRight, v3BackBottomRight, color);
Debug.DrawLine (v3FrontBottomLeft, v3BackBottomLeft, color);
//}
}
}
Thanks, but I want to be able to see the bounds in the editor window, preferably without having to use the box collider script.
Preferably I'd want the script to display exactly how the box collider displays given the same input. That way I can also keep the actual scale of the GameObject at (1,1,1).
Updated the question that will draw the bounds of the mesh.
You could do this with Gizmos :
always draw : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnDrawGizmos.html?from=Gizmos
draw when selected : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnDrawGizmosSelected.html
wireframe cube : http://docs.unity3d.com/Documentation/ScriptReference/Gizmos.DrawWireCube.html
@alucardj - Cool. It looks like DrawWireCube() is exactly what he is looking for.
great script but i had to comment out all the transform.TransformPoint lines to make it work. i wonder why?
because DrawLine draws in world coordinates - bounds are already in world coordinates - no need to transform them again.
Answer by chilemanga · Jul 10, 2017 at 07:13 PM
I've used Gizmos to accomplish a Box2DCollider type of visualization. In summary, you'll set its width and height in the Inspector, and the box will update its size while changing the values. Here's the code:
public class MyClassExampleBehaviour : MonoBehaviour {
[SerializeField]
float width = 0;
[SerializeField]
float height = 0;
void OnDrawGizmos() {
Gizmos.color = Color.yellow;
float wHalf = (width * .5f);
float hHalf = (height * .5f);
Vector3 topLeftCorner = new Vector3 (transform.position.x - wHalf, transform.position.y + hHalf, 1f);
Vector3 topRightCorner = new Vector3 (transform.position.x + wHalf, transform.position.y + hHalf, 1f);
Vector3 bottomLeftCorner = new Vector3 (transform.position.x - wHalf, transform.position.y - hHalf, 1f);
Vector3 bottomRightCorner = new Vector3 (transform.position.x + wHalf, transform.position.y - hHalf, 1f);
Gizmos.DrawLine (topLeftCorner, topRightCorner);
Gizmos.DrawLine (topRightCorner, bottomRightCorner);
Gizmos.DrawLine (bottomRightCorner, bottomLeftCorner);
Gizmos.DrawLine (bottomLeftCorner, topLeftCorner);
}
}
Answer by tanoshimi · Jul 19, 2014 at 07:57 PM
Read up about gizmos, particularly DrawWireCube : http://docs.unity3d.com/ScriptReference/Gizmos.html
Sorry - for some reason this appeared at the top of the questions list and the previous answer wasn't showing! Damn broken UA...
Answer by dan_wipf · Apr 16, 2020 at 03:45 PM
the most simplest way to display BB's. Meshrender's bb can be replaced with any BB.. =>
using UnityEngine;
[RequireComponent(typeof(MeshRenderer))]
public class DrawGizmoBB : MonoBehaviour
{
void OnDrawGizmosSelected()
{
// Draw a yellow cube at the transform position
Gizmos.color = Color.yellow;
var bb = GetComponent<MeshRenderer>().bounds;
Gizmos.DrawWireCube(bb.center, bb.size);
}
}
Answer by MrCowKing · Jul 19, 2014 at 08:04 PM
You should try my solution in the asset store. It's a lot less convoluted than the Gizmos version.
Your answer
Follow this Question
Related Questions
Percentage of Collider within a Trigger Area (C#) 1 Answer
Unity's Bounding Box 0 Answers
Enemy Detection Area 1 Answer
Size of collider bounding box regardless of rotation 1 Answer
Detecting object inside an area. 1 Answer