In Unity, a BoxCollider component has a center property and a size property. The center property describes the position of the collider's center relative to the GameObject's position, while the size property describes the dimensions of the collider along the GameObject's local axes.
Suppose you have a GameObject with a BoxCollider component and you want to determine the world positions of the opposite corners of the base, given a height from the base. Here's how you can do it:
First, get a reference to the BoxCollider component.
Calculate the local positions of the corners on the base using the center and size of the BoxCollider.
Use the Transform component's TransformPoint method to convert local positions to world positions.
Here's example code to do this:
csharp
using UnityEngine;
public class BoxColliderCorners : MonoBehaviour
{
void Start()
{
// Get the BoxCollider component
BoxCollider boxCollider = GetComponent<BoxCollider>();
// Calculate the half extents of the box
Vector3 halfSize = boxCollider.size * 0.5f;
// Calculate the base height offset from the center using the desired height from the base
float baseHeightOffset = -halfSize.y;
// Corner 1 (bottom-front-left)
Vector3 localCorner1 = boxCollider.center + new Vector3(-halfSize.x, baseHeightOffset, -halfSize.z);
Vector3 worldCorner1 = transform.TransformPoint(localCorner1);
// Corner 2 (bottom-front-right)
Vector3 localCorner2 = boxCollider.center + new Vector3(halfSize.x, baseHeightOffset, -halfSize.z);
Vector3 worldCorner2 = transform.TransformPoint(localCorner2);
// Corner 3 (bottom-back-left)
Vector3 localCorner3 = boxCollider.center + new Vector3(-halfSize.x, baseHeightOffset, halfSize.z);
Vector3 worldCorner3 = transform.TransformPoint(localCorner3);
// Corner 4 (bottom-back-right)
Vector3 localCorner4 = boxCollider.center + new Vector3(halfSize.x, baseHeightOffset, halfSize.z);
Vector3 worldCorner4 = transform.TransformPoint(localCorner4);
// Now you have the world positions of the four base corners
Debug.Log($"World Corner 1: {worldCorner1}");
Debug.Log($"World Corner 2: {worldCorner2}");
Debug.Log($"World Corner 3: {worldCorner3}");
Debug.Log($"World Corner 4: {worldCorner4}");
// If you want to draw lines between corners in the editor for visualization purposes:
Debug.DrawLine(worldCorner1, worldCorner2, Color.red, 5f);
Debug.DrawLine(worldCorner2, worldCorner4, Color.red, 5f);
Debug.DrawLine(worldCorner4, worldCorner3, Color.red, 5f);
Debug.DrawLine(worldCorner3, worldCorner1, Color.red, 5f);
}
}
Attach this script to the GameObject with the BoxCollider you're interested in. When you start the scene, it will calculate and log the world positions of the four bottom corners of the BoxCollider. It also optionally draws lines between those points in the editor for visual confirmation.
Keep in mind that if the GameObject is rotated, the world position calculations will take that into account, because TransformPoint converts the local position to world position based on the Transform component's rotation and scale as well. If your base is not aligned with the world axis, then the points will be rotated as well to match the game object's orientation.