Follow

Unity mystery:

1) In a new scene, create a cube. Give it scale 5,5,5, a transparent material, and make its BoxCollider a trigger.
2) Create a sphere inside the cube. Give it a RigidBody with no gravity.
3) Attach this script to the cube:
public class Hark : MonoBehaviour
{
private void OnCollisionStay(Collision other)
{
if (other.gameObject.name != "Ground")
{
Debug.Log(other.gameObject.name);
}
}
}
When run, shouldn't the cube detect the sphere inside it? Why doesn't it? How can I detect that?

@peterdrake I’m not at my machine right now, but my guess would be that default collision detection works by intersecting surfaces, so if the sphere is completely inside the cube, there’s no collision. I may be wrong, though.

@JensHannemann I think that's it. It turns out using OnTriggerStay instead does the trick.

@peterdrake Because when a trigger is involved, OnTriggerX gets invoked instead of OnCollisionX.

See the chart at the bottom of this page: docs.unity3d.com/Manual/Collid

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.