0
\$\begingroup\$

Currently I'm trying to let the player place a GameObject in the world. You can imagine this like a RTS game where you can place a building you have just build.
My problem now is, in order to avoid that the new GameObject intersects with another I check for collisions. For this my GameObjects have a BoxCollider (MeshCollider did not work) and a non kinematic RigidBody.
The first tests looked promising but now I'm facing a problem I can't quite explain: Sometimes, but not always OnCollisionEnter and OnCollisionExit are called over and over again, even when both objects are not moving. They overlap and stay that way. I had guessed that they always only get called once.
My Unity version is 5.3.0f4.

Further information: Each of my GameObjects have a guid so I can identify. They get generated when the GameOject is created. The debug output in OnCollisionEnter and Exit look like this:

OnCollisionEnter from 593b209f-cc63-4d31-9911-34c5f69ac224
Collided with: 77e05881-2adb-459d-a134-8205cf4e3327
OnCollisionEnter from 77e05881-2adb-459d-a134-8205cf4e3327!
Collided with: 593b209f-cc63-4d31-9911-34c5f69ac224
OnCollisionExit from 593b209f-cc63-4d31-9911-34c5f69ac224!
OnCollisionExit from 77e05881-2adb-459d-a134-8205cf4e3327!

And then the same output again.

I'm happy for any hints and help. :)

\$\endgroup\$
  • \$\begingroup\$ Can you explain further more about repetition and frequency of this happening? \$\endgroup\$ – Hamza Hasan Dec 13 '15 at 21:44
  • \$\begingroup\$ Are you modifying its transform.position every frame? Because this causes the Rigidbody to basically "leave" its current position and "enter" anew. If so, try using Rigidbody.MovePosition instead, which is the cleaner way to move an object that has a Rigidbody. \$\endgroup\$ – Chris McFarland Dec 13 '15 at 22:07
  • \$\begingroup\$ @HamzaHasan: This happens about every second time I place the object. The loop also repeats when the game object gets destroyed. I have no reference anywhere anymore and call Destroy on the game object. \$\endgroup\$ – Skalli Dec 13 '15 at 22:36
  • \$\begingroup\$ @Chris: No, the object's don't move. The only motion that might get applied (which I disabled for testing purposes but the problem kept happening) was RotateAround of the all the GameObjects within it's parent. No transform.position anywhere. \$\endgroup\$ – Skalli Dec 13 '15 at 22:38
  • 1
    \$\begingroup\$ @Chris: I've checked the RotateAround again and not only set rotation to 0 but commented out the line entirely. Now the problem seems not to be happening anymore. This might be the cause, I will do some further checks. \$\endgroup\$ – Skalli Dec 13 '15 at 22:46
1
\$\begingroup\$

Since I couldn't get it to work with OnCollisionEnter I've switched to using OnTriggerEnter instead which works fine for now. It only has a problem when the model is rotated 180° but that is another question.
To work with triggers the following steps have to be done (most of it was done in the same way with OnCollisionEnter):

  • Add RigidBody that has isKinematic set to true.
  • Add Collider (MeshCollider works too) that is Convex and a Trigger
| improve this answer | |
\$\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.