2D collision detection / box intersection WITHOUT physics

Hi all,

I’m using Unity 4.3 and C#.

I’m trying to write some 2D-only code to perform simple collision detection between non-physics-based sprites (i.e., no rigidbodies on anything). Obviously, because of the absence of rigidbodies, the OnCollision____2D() methods will not ever be called. Moreover, I’m working at a small enough scope that I actually want every-frame fidelity, not the reduced simulation framerate of FixedUpdate().

My question is this: is there a good way to leverage built-in Unity mechanics (again, all game objects involved are Sprites) to do super-easy collision detection either with Collider2D or some other built-in, or do I need to just write my own bounding box intersection test?

To be clear, I want to test the intersection of two bounding boxes, NOT a point with a bounding box. For now everything is axis-aligned, but that could definitely change.

Any ideas? Thanks!

What follows is the best I could come up with. If anybody has a better suggestion, please weigh in!

Colliders have no easy way of intersection without using the physics system. However, it’s easy to intersect Bounds objects with each other, and bounds are available from any renderer component. This code therefore does exactly what I needed:

if (object1.renderer.bounds.Intersects(object2.renderer.bounds)) {
  // Do some stuff

I don’t know the performance characteristics of getting renderer bounds, so it could be that this is not at all performant when scaled up to more objects.

Reasons I did not accept other suggestions

  1. I do not want to use Physics. Yes, I realize I could change the fixed step interval, but I a) don’t want the added overhead the system brings, and b) I don’t want physics, period! There are many cases where you need collision detection and physics is inappropriate. I want to be able to move things to specific screen coordinates by their transforms, not by figuring out IK and then applying the correct forces to get them there. I don’t want any objects affected by forces or collisions.

The built in mechanics for collisions comes from the physics system. So without a Rigidbody(2D) you don’t have any of that functionality. Unity used PhysX and the Rigidbody is the wrapper for it.

Keep in mind that you can change the Physics-timestep as you which. If you have enough processing power you could have 5000 PU/s (which would be around 100 physics steps per frame). So FixedUpdate does not run on a “reduced” rate, but on a fixed rate which ensures a certain amount of calls per second. If you want to ensure at least one physics update per frame you could do something like that:

void Update()
    Time.fixedDeltaTime = Time.deltaTime/2.0f;

That would doing around 2 physics steps per frame.

I didn’t use the 2D stuff yet (still using an older version of Unity), so i’m not sure if kinematic rigidbodies could cause collisions as well. If the Rigidbody2D is just a modified version of the usual RB probably not.

Other thsn that you might want to use Physics2D to do your own intersection checks.