selective OnCollision events

I have my collision matrix set up as tightly as I can to avoid collisions with things I do not care about. I have a conundrum though.

I don’t want to trigger collision events between my actors and my terrain, but at the same time I don’t want them to fall through the terrain. Is there a way to set up the physics such that these events do not fire for interactions with the terrain, but also will collide with the terrain?

If you don’t put a callback in a MonoBehaviour on the GameObject that has the Rigidbody or Collider then it won’t get called. You can’t have the things colliding and have a script callback set-up but ask that it’s never called (TBH that would make no sense).

I know what you are saying, but maybe my needs will make more sense if I go into more detail. Maybe I am going about it the wrong way, but anyways, here goes:

So I have two layers, Actor and Terrain. my Actor layer includes objects that move around on the terrain, and Terrain is…well… terrain.

I set up OnCollisionEnter because when an Actor collides with another Actor, I want to take a particular action (avoid, attack, etc). Of course, what is happening is that this is firing every frame because of the terrain. I also found that I was missing collision events for other Actors because it was busy getting collision events for the Terrain.

Personally, after using Unity on and off for a few years now, I have never really found these collision events to be all that useful because they are too restrictive. I almost always end up just firing a ton of raycasts to do anything. I really wish there was not only a collision matrix, but an event matrix. It is pretty useless without that.

Anyways, now that you see what my motivation is; do you have any suggestions? The only thing I can really think of is to not use gravity at all, and remove terrain from the collision matrix altogether. That seems silly though because it means if one wants to use gravity they pretty much can’t use collision events. Maybe I am wrong though?

I don’t know what to suggest. The callback is for the collider/rigidbody in question and not a particular combination of colliders or combination of layers.

What confuses me is that you say it’s being called each frame. I presume you don’t mean each render frame because that isn’t the case. Also, you’d only get OnCollisionEnter once when the object collided with the terrain, not continuously so again, I don’t see the problem. Your collision events with the terrain should be low frequency.

Hmm. Maybe I was misunderstanding the frequency I was getting them. I guess the best bet for me is to simply test the layer I am colliding with, and to ignore it if it is one I do not care about.