using UnityEngine;
using System.Collections;
public class GetBounds : MonoBehaviour
{
Vector3 targetPoint = Vector3.zero;
public void OnDrawGizmos()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Bounds bounds = mesh.bounds;
// not affected by scale or rotation
Debug.Log ("x: " + bounds.size.x +" y: "+ bounds.size.y + " z: " + bounds.size.z);
// next you can simply calculate the start point of the ray
Vector3 edgeA = transform.position + new Vector3(bounds.size.x * transform.localScale.x / 2, 0, 0);
Vector3 edgeB = transform.position - new Vector3(bounds.size.x * transform.localScale.x / 2, 0, 0);
Vector3 edgeC = transform.position + new Vector3(0, 0, bounds.size.z * transform.localScale.y / 2);
Vector3 edgeD = transform.position - new Vector3(0, 0, bounds.size.z * transform.localScale.y / 2);
// next if plane is rotated you must calculate position for this rotation...
edgeA = RotatePointAroundPivot(edgeA, transform.position, transform.localRotation.eulerAngles);
edgeB = RotatePointAroundPivot(edgeB, transform.position, transform.localRotation.eulerAngles);
edgeC = RotatePointAroundPivot(edgeC, transform.position, transform.localRotation.eulerAngles);
edgeD = RotatePointAroundPivot(edgeD, transform.position, transform.localRotation.eulerAngles);
Gizmos.color = Color.red;
Gizmos.DrawLine(edgeA, targetPoint);
Gizmos.DrawLine(edgeB, targetPoint);
Gizmos.DrawLine(edgeC, targetPoint);
Gizmos.DrawLine(edgeD, targetPoint);
}
private Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
{
Vector3 dir = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point; // return it
}
}