Which Object Should Hold Collision Logic?

Hey all,

I just wanted to put this question out there to get a sense of what the most common practices are. To go in depth further:

If two objects with colliders hit each other, they both can fire the OnCollision events. So my questions is, which object do you use to put unique collision logic? Is there a rule of thumb you use to follow? The example I am working with currently is I have a Character that can walk over a Weapon. When that collision occurs, the Character will pick the Weapon up automatically. So would it make more sense for me to put this logic in the Character script or the Weapon script?

Or maybe I’m thinking too narrowly for this. Should it be abstracted out into a script of its own and put in that?

Just curious how most others structure their games.

Best.

1 Like

There’s no rule, and as long as you are consistent it should be fine. You may very well handle a collision on both sides, for example when two objects bang in to each other they may both emit a sound based on their material.

From a design point of view it arguably makes more sense for characters to know about weapons than for weapons to know about characters, but sometimes this can just add complexity that isn’t necessary. A component based approach might suggest that neither know about each other and there is a third component called for example CharacterWeapon which handles the collection, selection and shooting of weapons.

Start simple, keep code clean and responsibilities well defined, and don’t fear refactoring if the need arises.

1 Like

Case by case.

In singleplayer, it doesn’t matter. In multiplayer, I would handle this on the gun. The gun is owned by the server so I think it would know best what’s going on.

That’s just my opinion. In multiplayer environments, I usually have objects that act with players instead of players acting on objects… if that makes sense.

1 Like

The biggest consideration I would have is where might I change the logic. If one mob uses a completely different model for whatever, then it only makes sense for this interaction to take variable logic into account.

One thing to think about it that the collision detection and the bulk of the logic don’t have to be on the same object. The weapon may have the collision event, but the character would be responsible for figuring out whether it wants to pick it up.

1 Like

I normally do a combination of both. For my average damage systems, it’s the object doing damage that picks up the original collision. So the sword rather then the enemy.

The sword then passes on a damage message/event to the damage receiver. That way the damage receiver can apply armour and the like as it pleases.

3 Likes