Okay, so usually using the older rigidbody system, I would attach a rigidbody to my colliding object (the one that had OnCollision method in it) and check all the freeze position and freeze rotations. Now I’m trying to switch my current project to using 2D physics since it is a 2D game. The problem I’m having is that there is no way to put the rigidbody 2D on an object without it reacting to physics. So I’ve tried a few things and I just want to be sure I’m doing it the best way. The way I’m leaning towards right now is to simply switch the Box Collider 2D to a trigger and use OnTriggerEnter2D instead of OnCollisionEnter2D. Only problem I have with that solution is that I won’t be able to have access to the contact point using the collision parameter.
So my question is, how can I detect collision between 2 sprites with box collider 2D components, without having the physics system also effect my objects?
" The problem I’m having is that there is no way to put the rigidbody 2D on an object without it reacting to physics."
Incorrect
Is Kinematic
If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached.
Thanks for the reply. However, I've already tried checking "isKinematic". When is kinematic is checked, OnCollisionEnter2D did not work, nor did OnTriggerEnter2D. I've read the documentation about IsKinematic and if it worked for me it seemed like it would have been perfect. If I did something wrong and it should be working, it would be nice if somebody could explain it to me.
Actually just to clarify, I think I may have been a little unclear. The problem isn't exactly that I can't put a rigidbody2D on an object without it reacting to physics, it's that I can't put a rigidbody2D on an object without it reacting to physics, yet still be able to call OnCollision/OnTrigger.
I'm trying it right now, with IsKinematic checked on the Rigidbody2D component, and IsTrigger checked on the BoxCollider2D, the OnTriggerEnter2D method is not being called. Under the same scenario, but with IsKinematic unchecked, OnTriggerEnter2D is called. I would prefer if I could get OnCollisionEnter2D to be called. But if I uncheck IsTrigger, then my object will collide and react to physics, which is the basis of this question. I want to be able to collide and OnCollisionEnter2D to be called, but I don't want the physics to happen.
Are both objects checked as IsTrigger? And if you aren't having the physx subsystem handle object movement then it will never generate a collision - that's just how it goes.
That seems like a pretty useful feature. Only problem I have with it is it seems you must know the 2 objects you are checking against. I'm using a projectile which is hitting enemies. So I don't know what enemy it will hit.
Then you have to find all the gameObjects in your scene by script, and then check ALL their bounding boxes for collision. If there is a collision, you can check between what two GameObjects the collision occurs. That's how collision detection works on the most primitive level. It works fine for games that don't have too many objects flying around the screen.
Thanks for the reply. However, I've already tried checking "isKinematic". When is kinematic is checked, OnCollisionEnter2D did not work, nor did OnTriggerEnter2D. I've read the documentation about IsKinematic and if it worked for me it seemed like it would have been perfect. If I did something wrong and it should be working, it would be nice if somebody could explain it to me.
– JopanActually just to clarify, I think I may have been a little unclear. The problem isn't exactly that I can't put a rigidbody2D on an object without it reacting to physics, it's that I can't put a rigidbody2D on an object without it reacting to physics, yet still be able to call OnCollision/OnTrigger.
– JopanThey won't generate collisions but should still generate triggers as long as they're marked as triggers.
– DracoratI'm trying it right now, with IsKinematic checked on the Rigidbody2D component, and IsTrigger checked on the BoxCollider2D, the OnTriggerEnter2D method is not being called. Under the same scenario, but with IsKinematic unchecked, OnTriggerEnter2D is called. I would prefer if I could get OnCollisionEnter2D to be called. But if I uncheck IsTrigger, then my object will collide and react to physics, which is the basis of this question. I want to be able to collide and OnCollisionEnter2D to be called, but I don't want the physics to happen.
– JopanAre both objects checked as IsTrigger? And if you aren't having the physx subsystem handle object movement then it will never generate a collision - that's just how it goes.
– Dracorat