6
\$\begingroup\$

I am trying to detect a collision between 2 objects, each one of them has a Collider component. In the script I have the method OnCollisionEnter(Collision other) where I want to check if the collided object is the same as a pre-defined variable, but when they collide, nothing gets called.

If I try to attach a Rigidbody to one of the objects, it works, but that makes the Rigidbody object behaves weird, like going up and down, and changing its rotation.

Is there a way to check for collision without having a Rigidbody attached? Or if not, how can I make the object with Rigidbody not moving weirdly, and rotating?

\$\endgroup\$
5
\$\begingroup\$

Rigidbody.isKinematic

If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position. Kinematic bodies also affect the motion of other rigidbodies through collisions or joints.

You can toggle this on/off in the Inspector, or in code

GetComponent<Rigidbody>().isKinematic = true;

Caution: having Colliders without a Rigidbody is never a good idea unless the object in question is static. If an object without a Rigidbody moves, it's Collider will not move with it!

\$\endgroup\$
  • \$\begingroup\$ Actually if an object without a Rigidbody moves, it's Collider will move with it, it just wont work exactly as you would expect it. \$\endgroup\$ – zoran404 Jul 15 '15 at 22:41
  • 1
    \$\begingroup\$ In Unity4 (and less) moving colliders without rigidbodies had a big hit on performance. Not very high if you only had like 20 colliders, but prohibitive if you had 1000+. As of Unity5 this is no longer the case - moving colliders does not have any special cost now. They still don't detect collisions without rigidbodies, though. \$\endgroup\$ – Nevermind Aug 5 '15 at 18:41
1
\$\begingroup\$

Moving an object without a rigidbody or a character controller is also not optimized and will slow your game down, you can check Is Kinematic if you don't want forces acting on it and it will still be more optimized

\$\endgroup\$
0
\$\begingroup\$

An ad-hoc solution to this problem is to work with Triggers (eg OnTriggerEnter2D) and restrict Rigidbody constraints. So even if the collision occurs, it will not have an effect to the physics if the appropriate rotations and/or positions are "freezed"

if for example you want the object to move only along the 'X' axis and rotate on the 'Y' axis. You would do:

rigidbody.constraints = RigidbodyConstraints.FreezeRotationX |
RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionY |
RigidbodyConstraints.FreezePositionZ;

Otherwise you can work with the intersection of the bounds:

var bounds1 = gameObject1.renderer.bounds;
var bounds2 = gameObject2.renderer.bounds;

if (bounds1.Intersects(bounds2))
{
    // do something
}

the problem with that is that it has to be placed in a coRoutine or inside the Update() method.

Hope it helps!

\$\endgroup\$

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.