Problems with Rendering 3D Bounding Box at Runtime

Hello everyone. I’ve been trying to render a bounding box at runtime in Unity with GL.LINES. I have two questions (problems) with my solution thus far. For one, the box seems to be transformed wrong. I can’t seem to pin this down. Two, I’ve been trying to get the box to update as the object is moved around, but no such luck. This doesn’t make sense to me either, as I thought transforming the points into world space would accomplish this.

Here’s the somewhat hackish code I’m using:
using UnityEngine;
using System.Collections.Generic;

public class RenderBoundingBox : MonoBehaviour
{
    [SerializeField]
    private List<Vector3> boundingBoxPoints;

    [SerializeField]
    private Mesh boundsMesh;

    [SerializeField]
    private Bounds bounds;

    public Material lineMaterial;

    /// <summary>
    /// Ordered points of the bounding box
    /// </summary>
    public List<Vector3> BoundingBoxPoints
    {
        get
        {
            return boundingBoxPoints;
        }

        set
        {
            boundingBoxPoints = value;
        }
    }

    private void Awake()
    {
        // Get the mesh and the mesh's bounds object
        boundsMesh = GetComponent<MeshFilter>().mesh;
        bounds = boundsMesh.bounds;
    }

    private void LateUpdate()
    {
        // Get the bounding box points
        BoundingBoxPoints = CalculateBoundingBoxCorners(bounds);

        // Transform all the points into world space
        for (int i = 0; i < BoundingBoxPoints.Count; i++)
        {
            BoundingBoxPoints _= transform.TransformPoint(BoundingBoxPoints*);*_

}
}

void OnRenderObject()
{
if (!lineMaterial)
{
Debug.Log(“Unable to render lines”);
return;
}

lineMaterial.SetPass(0);

GL.Begin(GL.LINES);

// Points from the first point
GL.Vertex3(BoundingBoxPoints[0].x, BoundingBoxPoints[0].y, BoundingBoxPoints[0].z);
GL.Vertex3(BoundingBoxPoints[1].x, BoundingBoxPoints[1].y, BoundingBoxPoints[1].z);

GL.Vertex3(BoundingBoxPoints[0].x, BoundingBoxPoints[0].y, BoundingBoxPoints[0].z);
GL.Vertex3(BoundingBoxPoints[3].x, BoundingBoxPoints[3].y, BoundingBoxPoints[3].z);

GL.Vertex3(BoundingBoxPoints[0].x, BoundingBoxPoints[0].y, BoundingBoxPoints[0].z);
GL.Vertex3(BoundingBoxPoints[4].x, BoundingBoxPoints[4].y, BoundingBoxPoints[4].z);

// Points from the second point
GL.Vertex3(BoundingBoxPoints[1].x, BoundingBoxPoints[1].y, BoundingBoxPoints[1].z);
GL.Vertex3(BoundingBoxPoints[5].x, BoundingBoxPoints[5].y, BoundingBoxPoints[5].z);

GL.Vertex3(BoundingBoxPoints[1].x, BoundingBoxPoints[1].y, BoundingBoxPoints[1].z);
GL.Vertex3(BoundingBoxPoints[2].x, BoundingBoxPoints[2].y, BoundingBoxPoints[2].z);

// Points from the third point
GL.Vertex3(BoundingBoxPoints[3].x, BoundingBoxPoints[3].y, BoundingBoxPoints[3].z);
GL.Vertex3(BoundingBoxPoints[7].x, BoundingBoxPoints[7].y, BoundingBoxPoints[7].z);

GL.Vertex3(BoundingBoxPoints[3].x, BoundingBoxPoints[3].y, BoundingBoxPoints[3].z);
GL.Vertex3(BoundingBoxPoints[2].x, BoundingBoxPoints[2].y, BoundingBoxPoints[2].z);

// Points from the fourth point
GL.Vertex3(BoundingBoxPoints[4].x, BoundingBoxPoints[4].y, BoundingBoxPoints[4].z);
GL.Vertex3(BoundingBoxPoints[7].x, BoundingBoxPoints[7].y, BoundingBoxPoints[7].z);

GL.Vertex3(BoundingBoxPoints[4].x, BoundingBoxPoints[4].y, BoundingBoxPoints[4].z);
GL.Vertex3(BoundingBoxPoints[5].x, BoundingBoxPoints[5].y, BoundingBoxPoints[5].z);

// Points from the fifth point
GL.Vertex3(BoundingBoxPoints[5].x, BoundingBoxPoints[5].y, BoundingBoxPoints[5].z);
GL.Vertex3(BoundingBoxPoints[6].x, BoundingBoxPoints[6].y, BoundingBoxPoints[6].z);

// Points from the sixth point
GL.Vertex3(BoundingBoxPoints[6].x, BoundingBoxPoints[6].y, BoundingBoxPoints[6].z);
GL.Vertex3(BoundingBoxPoints[2].x, BoundingBoxPoints[2].y, BoundingBoxPoints[2].z);

GL.Vertex3(BoundingBoxPoints[6].x, BoundingBoxPoints[6].y, BoundingBoxPoints[6].z);
GL.Vertex3(BoundingBoxPoints[7].x, BoundingBoxPoints[7].y, BoundingBoxPoints[7].z);

GL.End();

}

///


/// Calculates the corners of the bounding box given by the
///

/// This is the bounds to use for the calculation
/// Returns a list of points that represent the corners.
public static List CalculateBoundingBoxCorners(Bounds bounds)
{
//POSITIVE Z

//1,1,1
Vector3 v1 = new Vector3(bounds.size.x + bounds.extents.x,
bounds.size.y + bounds.extents.y,
bounds.size.z + bounds.extents.z);

//-1,1,1
Vector3 v2 = new Vector3(bounds.size.x - bounds.extents.x,
bounds.size.y + bounds.extents.y,
bounds.size.z + bounds.extents.z);

//-1,1,1
Vector3 v3 = new Vector3(bounds.size.x - bounds.extents.x,
bounds.size.y - bounds.extents.y,
bounds.size.z + bounds.extents.z);

//1,-1,1
Vector3 v4 = new Vector3(bounds.size.x + bounds.extents.x,
bounds.size.y - bounds.extents.y,
bounds.size.z + bounds.extents.z);

//NEGATIVE Z

//1,1,-1
Vector3 v5 = new Vector3(bounds.size.x + bounds.extents.x,
bounds.size.y + bounds.extents.y,
bounds.size.z - bounds.extents.z);

//-1,1,-1
Vector3 v6 = new Vector3(bounds.size.x - bounds.extents.x,
bounds.size.y + bounds.extents.y,
bounds.size.z - bounds.extents.z);

//-1,-1,-1
Vector3 v7 = new Vector3(bounds.size.x - bounds.extents.x,
bounds.size.y - bounds.extents.y,
bounds.size.z - bounds.extents.z);

//1,-1,-1
Vector3 v8 = new Vector3(bounds.size.x + bounds.extents.x,
bounds.size.y - bounds.extents.y,
bounds.size.z - bounds.extents.z);

List points = new List();

points.Add(v1);
points.Add(v2);
points.Add(v3);
points.Add(v4);
points.Add(v5);
points.Add(v6);
points.Add(v7);
points.Add(v8);

return points;
}
}
And it produces a perfect cube/rectangle… but it’s just “off”. Here’s a screenshot of what I mean:
[15486-badboundingbox.png|15486]_
It works regardless of scaling/mesh too:
_
[15487-badboundingboxlarger.png*|15487]*
Any ideas of what I’m doing wrong, and/or how to make this code update as the object moves around?
_*
_*

Replace bounds.size with bounds.center. The TransformPoint() method will transform a point in space, but “size + extents” does not move when the box moves, so the point doesn’t move.