- Home /
How can I detect collision without physics?
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?
Answer by AndreiKubyshkin · Nov 15, 2013 at 02:54 PM
I'm very beginner to unity and maybe my solution is somewhat ugly, but it worked for me:
on a "trigger" use collider2D with "is trigger" turned on
on object you want to check, whether it is in trigger, use collider2D with "is trigger" turned on, and "RigidBody" with gravity scale set to zero.
Answer by ruslux · Nov 19, 2013 at 07:48 PM
gameObject1 with any collider + gameObject2 with any collider with 'isTrigger' and script attached to gameObject2 work perfectly:
void OnTriggerEnter2D(Collider2D other) {
if (other.collider2D.tag == "Tire") {
//your code
}
}
P.S. Use tag "Player" on gameObject1
Answer by meat5000 · Nov 13, 2013 at 11:16 PM
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.
Answer by Dracorat · Nov 13, 2013 at 10:53 PM
" 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
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html
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 "is$$anonymous$$inematic". When is kinematic is checked, OnCollisionEnter2D did not work, nor did OnTriggerEnter2D. I've read the documentation about Is$$anonymous$$inematic 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.
They won't generate collisions but should still generate triggers as long as they're marked as triggers.
I'm trying it right now, with Is$$anonymous$$inematic checked on the Rigidbody2D component, and IsTrigger checked on the BoxCollider2D, the OnTriggerEnter2D method is not being called. Under the same scenario, but with Is$$anonymous$$inematic 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.
Your answer
Follow this Question
Related Questions
raycast hit point is wrong 2 Answers
Camera looking through objects when touching 1 Answer
get angular velocity change from collision 0 Answers
No OnCollisionEnter with 2D colliders? 1 Answer
How do I increase speed after collision? 2 Answers