Recommended approach to triggering collisions in 2d Platformer?

Hi All,

I’ve implemented the beginnings of my engine using raycast2d after following several different tutorials on the premise and my player collision is working ok.

I’ve moved on to coding my hazards and interactive objects and what I wanted to do was have any object encountering another call an “interactable” interface. For example I’m the player and I hit a hazard, I want to call hazard.interact(playerReference).

I can get this behaviour by setting a kinematic RigidBody2D on the player and using the “OnCollisionEnter” tag but this feels wrong since I’ve worked without rigidbody so far.

I can also use the raycast2d hit to check what the player is going to interact with by looping through that gameobject and checking for something which implements “interactable”, however I think this would be very heavy due to the possible amount of casts with each cast looping through the scripts attached to a gameobject to see if one of them is “interactable”

I could use tags but that restricts me to one type per object where I feel the interface approach is much more flexible.

I’m just on hazards and switches currently but I’m also thinking ahead to enemies and I would like enemies to sometimes be able to interact with hazards etc. which is why I’m trying to keep the code generic and modular.

Any input at all is greatly appreciated

You can check for a component that implements an interface using GetComponent(), it’s not that expensive if you do it only during a successful raycast or collision event, and you’re not just detecting all collisions.