I am trying to make a game where player will be placed in the scene and the goal is to explore it. He can interact with game objects that player can find in the environment, like picking object, examining, dropping and putting back. Basically it is something like first person exploration...

So the problem is how to properly implement code regarding interaction. One way I see and saw it on the internet is Raycasting. So the idea would be to in Update() method place Raycast fire code and see what it hits. Also all game objects with which player can interact are placed on separate Layer and also limit length of Raycast (which is obvious thing to do as player cannot interact with objects that are like 10 meters from him)...

So as I understand, game objects should have at least some kind of collider (I know box and sphere colliders are simple, but sometimes I will need to use more complex one)...

That being said, I wonder is this right approach:

  • Separate layer for interactable objects
  • Raycast that checks objects in that layer
  • Raycast length limited to like 1 meter

This approach should not be that much CPU consuming or is there any other way?

I am also thinking to putting some invisible box area (trigger) where raycasting would make sense, but I am not sure will this be overhead or not....

So, at the end what would be the right approach to this?

  • \$\begingroup\$ Yeah, this is pretty much the way to go. \$\endgroup\$ – Iggy Aug 9 '19 at 12:54
  • \$\begingroup\$ It sounds like you have some workable ideas here to try. You do not need our permission to go try them. If you run into a problem, that's when you should ask here. StackExchange is best for detailed answers to questions like "how?" not questions that can be answered with "yes" or "no" or by your own tests. \$\endgroup\$ – DMGregory Aug 9 '19 at 13:20
  • 2
    \$\begingroup\$ The idea that there's a secret "right" or "proper" way to implement a game feature, and you can't start until someone tells it to you, is frankly only going to hold you back. Just dive in and make your game. Solve only the real problems you encounter, not imaginary problems that haven't happened yet. \$\endgroup\$ – DMGregory Aug 9 '19 at 13:28

Browse other questions tagged or ask your own question.