Found a solution if anyone is interested:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using MathGeoLib;
public class Intersection : MonoBehaviour
{
public GameObject m_MyObject, m_NewObject;
Collider m_Collider, m_Collider2;
private List<Vector3> inboundsVertices = new List<Vector3>();
private bool drawVertGizmos = false;
[SerializeField]
private OrientedBoundingBox obb;
void Start()
{
//Check that the first GameObject exists in the Inspector and fetch the Collider
if (m_MyObject != null)
m_Collider = m_MyObject.GetComponent<Collider>();
//Check that the second GameObject exists in the Inspector and fetch the Collider
if (m_NewObject != null)
m_Collider2 = m_NewObject.GetComponent<Collider>();
}
void Update()
{
float xAngle = m_NewObject.transform.eulerAngles.x;
float yAngle = m_NewObject.transform.eulerAngles.y;
float zAngle = m_NewObject.transform.eulerAngles.z;
Quaternion rotation = Quaternion.Euler(xAngle, yAngle, zAngle);
Matrix4x4 m = Matrix4x4.Rotate(rotation);
Vector3 xRotation = new Vector3(m.m00, m.m10, m.m20);
Vector3 yRotation = new Vector3(m.m01, m.m11, m.m21);
Vector3 zRotation = new Vector3(m.m02, m.m12, m.m22);
obb = new OrientedBoundingBox(m_Collider2.bounds.center, m_NewObject.transform.localScale / 2, xRotation, yRotation, zRotation);
//If the first GameObject's Bounds enters the second GameObject's Bounds, output the message
if (m_Collider.bounds.Intersects(m_Collider2.bounds))
{
CheckVertices(m_MyObject, obb);
drawVertGizmos = true;
}
}
void OnDrawGizmos()
{
if (drawVertGizmos)
{
DrawVertexGizmos();
//DrawBoundingBox();
}
}
void DrawBoundingBox()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireCube(m_NewObject.transform.position, m_Collider2.bounds.size);
}
void DrawVertexGizmos()
{
foreach (Vector3 vertex in inboundsVertices)
{
Gizmos.DrawSphere(vertex, 0.04f);
}
}
void CheckVertices(GameObject obj, OrientedBoundingBox bounds)
{
inboundsVertices.Clear();
if (obj == null)
return;
MeshFilter mf = obj.GetComponent<MeshFilter>();
if (mf == null)
return;
Vector3[] verticesToCheck = obj.GetComponent<MeshFilter>().mesh.vertices;
foreach (Vector3 vertex in verticesToCheck)
{
Vector3 pos = obj.transform.TransformPoint(vertex);
if (bounds.Contains(pos))
{
inboundsVertices.Add(pos);
}
}
}
}
Here is the result:
