x


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
more ▼

asked Feb 10 '12 at 08:31 PM

numberkruncher gravatar image

numberkruncher
1.9k 36 46 57

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
    }
}
more ▼

answered Feb 22 at 03:38 PM

numberkruncher gravatar image

numberkruncher
1.9k 36 46 57

(comments are locked)
10|3000 characters needed characters left

Try using gameObject.renderer.bounds.Intersects

more ▼

answered Feb 22 at 03:18 PM

privatecs gravatar image

privatecs
1

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!

Feb 22 at 03:39 PM numberkruncher
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2485
x1956
x1686
x662
x19

asked: Feb 10 '12 at 08:31 PM

Seen: 1207 times

Last Updated: Feb 22 at 03:40 PM