- Home /
How to perform intersection test between two colliders?
I have many enemy objects in a game that I am developing for iOS. I have found that colliders are using excessive processing capacity.
In many cases I only require collision checks between enemy and player. There is no need for enemies to be checked against anything other than the player.
So... I have replaced colliders with basic distance testing (in update script) and this has led to massive performance improvements. The problem is that the player is not spherical so collision detection of player is poor.
How can I manually test for intersection using either:
Two actual colliders (where one is disabled)
One actual collider and one sphere radius + position
Answer by numberkruncher · Feb 22, 2013 at 03:38 PM
I have been using the following and it seems to be working for me:
public float collisionRadius = 1.1f;
private GameObject _player;
void Start() {
_player = GameObject.FindGameObjectWithTag("Player");
}
void Update() {
Vector3 playerPoint = _player.collider.ClosestPointOnBounds(transform.position);
float playerRadius = Vector3.Distance(_player.transform.position, playerPoint);
if (Vector3.Distance(transform.position, _player.transform.position) <= collisionRadius + playerRadius) {
// Do something!
GameObject.Destroy(gameObject);
}
}
Answer by privatecs · Feb 22, 2013 at 03:18 PM
Try using gameObject.renderer.bounds.Intersects
Thanks for your suggestion, though this will only perform bounds checking on the bounding boxes of the renderer (if there is a renderer).
The approach that I later found and added to my question seems to work fine (so far!). I have moved this into an answer because it seems to be correct. Though anybody please feel free to provide feedback!