What I’m trying to do is create an “onInteract” event. The way I wanted to implement this in my 3D game is by using a raycast in front of the player and store its hit in a variable. However, since not every object is interactable (that is, not every object has an “IInteractable” interface attached), I think I have to use hit.getComponent() but I’ve read that’s an expensive call to use in the update method. Is there any other way I can check whether the RaycastHit has the IInteractable interface?
You can use tags to avoid using .getComponent
unnecessarily.
Just add a tag named “Interactable” or sth like that, add that tag to the interactable object, then check using if(rayHit.collider.CompareTag("Interactable"))