How do i fix this common problem where an object that's attatched to the main camera will go through walls. I am makeing an fps, How do I stop the the gun from going through objects.

Here's a good solution, although it's slightly involved. Do your best to follow each step exactly.

  1. Create a new layer by clicking on Layers/Add Layer from any object. Name it "GunLayer".
  2. Put your gun on this layer by clicking Layers/GunLayer from the Layers dropdown while on the gun object.
  3. Create a new camera, and uncheck all the components except for the Camera component. Name it "Gun Camera"
  4. Then, still on the Gun Camera object, select the culling mask dropdown, and select GunLayer. This makes it so the camera will render only objects on the GunLayer layer.
  5. Set Clear Flags to Depth Only. This means it won't render a background or skybox or anything, just the gun.
  6. Move your Gun Camera object to the exact same coordinates as the main camera, and make it a child of the main camera. Getting it to the exact same coordinates is crucial.

That's it, your gun will no longer go through walls.