Reputation:
I have what I think is a straight forward question, but my lack of unity knowledge is hindering me.
I have primitive type game objects (e.g. a cube / capsule / sphere) rotated at various angles in my scene. They are already instantiated.
I then add a new game object primitive type to the scene and I want to know if this new game object overlaps / intersects the current primitive types in the scene.
To do this I have been using the gameobject1.GetComponent<Collider>.bounds.Intersects(gameobject2.GetComponent<Collider>.bounds)
However this doesn't work because the bounds is a bounding box so even if a game object isn't directly touching one of the primitive type game objects visually, if it's in it's bounding box, it returns that as true (i.e. they intersect) which is not what I want:
I'm sure there is another way of doing this in unity but I just don't know what I need to do!
Any help would be appreciated. Thanks
Upvotes: 3
Views: 25849
Reputation: 16277
Use MeshCollider, instead of BoxCollider
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
}
Upvotes: 2
Reputation: 646
You could use a MeshCollider instead (as long as your objects are simple) then set the MeshCollider IsTrigger property to true.
Then in code you can do:
void OnTriggerEnter(Collider col)
{
}
or
void OnCollisionEnter(Collision col)
{
}
When ever an object enters the collider, the above function will be called and you can do what you need to there.
Edit: Your objects will need a rigidbody added to them for the collision to work
Upvotes: 1