1
\$\begingroup\$

I'm fairly new to the world of Unity, and am adding collision detection for the first time through RigidBody2D (as this game is 2D). First of all, here is some relevant information:

  • Unity Version: 4.2.5f
  • Operating System: Windows 8.1 Update 1

The Problem

I have two objects that could collide, let's call one Bullet and one Enemy. Both Bullet and Enemy have RigidBody2D components attached to them, as well as the following method in an attached script:

void OnCollisionEnter2D(Collision2D collision){
    print("Hit!");
}

However, nothing is printed to the console (I have checked that print works in other situations).

The Question

What is going wrong here? I must be missing something simple. I have checked that Physics 2D has collisions enabled between layers, I have tried the 3D equivalent to this and still no joy.

Additional Info

Enemy

Bullet

\$\endgroup\$
3
\$\begingroup\$

You need to add a BoxCollider2D component to your GameObjects.

http://docs.unity3d.com/ScriptReference/BoxCollider2D.html

From the Unity docs for OnCollisionEnter2D

Sent when an incoming collider makes contact with this object's collider

A Rigidbody2D just tells a GameObject how to interact with the physics engine. It doesn't provide a collider for collision.

From the Unity docs for RigidBody2D

By adding the appropriate collider component, the sprite will also respond to collisions with other sprites.

\$\endgroup\$
  • \$\begingroup\$ Ah thank you, I must have misread something as I thought RigidBody2D was an alternative rather than supplement! I'll mark this as a solution for now as it makes perfect sense to me, although I'm unable to test at present. Thanks again! \$\endgroup\$ – OMGtechy Jul 25 '14 at 18:52
  • \$\begingroup\$ Confirmed to work (I'd also missed off the script for Enemy). Thank you! \$\endgroup\$ – OMGtechy Jul 26 '14 at 16:16
0
\$\begingroup\$

The problem could be either:

  1. You've not marked your moving rigidbody with the right collision detection method. You need continious dynamic rather than discrete if you are moving your object with speed.

  2. Are both rigidbodies marked as kinematic? If so the event won't call.

\$\endgroup\$
  • \$\begingroup\$ 1) There is no continious dynamic option, but there is continious so I tried that. Sadly it didn't make any difference, and the objects are not travelling so fast that I'd think they'd require it, but it was worth a try! 2) Neither are kinematic. Thank you for your efforts so far :) \$\endgroup\$ – OMGtechy Jul 25 '14 at 15:48
  • \$\begingroup\$ Are either colliders marked as triggers and at the right size? And do the colliders actually collide, i.e does one bounce off the other? \$\endgroup\$ – Lucas Jul 25 '14 at 15:50
  • \$\begingroup\$ I cannot see a trigger option anywhere, and they do not bounce off each other either (although once this would be desired behavior when the implementation is feature complete as I'd like both objects to be destroyed) \$\endgroup\$ – OMGtechy Jul 25 '14 at 15:52
  • \$\begingroup\$ Would it help if I posted an image with all the components attached to the object (alongside their settings)? :) \$\endgroup\$ – OMGtechy Jul 25 '14 at 15:58
  • \$\begingroup\$ Yes, that would help :) Also, try decreasing the speed of the bullet. It's possible it's traveling so fast that there is no collision occurring between the objects (are they actually touching?). \$\endgroup\$ – Cooper Jul 25 '14 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.