- Home /
Duplicate Question: http://answers.unity3d.com/questions/39420/gun-goes-through-wall-in-a-fps.html
FPS gun clippes through walls
My gun goes through the walls I have a game object box with a mesh collider on it bit it. It did not fix my problem. Could somebody tell me to add a collider that is something other than a mesh collider or box collider
Thanks
Please use proper grammar when asking a question here. You will be respected here much more if you do that, thanks :)
Fixed it. Voted to close since it pops up so much. Just chose the first dupe I could find.
And if you have the AimDownSight script attached to the $$anonymous$$ain Camera, in main camera culling masks don't forget to disable the "Gun" Layer so when you aim you don't have wierd things... ^^
Answer by Zylar · Jan 07, 2011 at 05:42 AM
The following was part of an answer I made to address another question.
I'll put in that last bit because it's a sweet trick to know.
Avoiding a Clipped Gun
This will have the gun still visible even though it passes though walls and objects.
- Add a new layer and name it "Gun Layer" or equivalent.
- Place your gun in the gun layer.
- Create a new camera and rename it "Gun Camera" or equivalent. [ GameObject -> Create Other -> Camera ]
- Remove the gun camera's audio listener, flare layer, and gui layer components. They will be handled by the Main Camera.
- Make the camera component's clear flags Depth Only, culling mask only the gun layer, and change the depth to be higher than the main camera's.
Adjusting the Crosshair
This is a pretty neat trick to finding the center of your screen, and thus moving your crosshair to the right place.
- Switch to the game view. The larger your game view the more accurate your placement will be.
- On your Gun Camera change its field of view to maximum. This will force the perspective to squeeze your gun directly into the center of the screen! My gun turned into one pixel.
- Now using your gun as the center, position your crosshair so that its center is pointed exactly at the single pixel (The gun). Also, because of the gun layer camera, the gun will be drawn on top of the crosshair. So long as the gun's single pixel can stand out when compared to the crosshair, you will see it.
- Once done placing the crosshair, go back and return the gun camera's field of view to normal (Roughly 60).
And there you have it! By now you should have:
- A crosshair centered exactly at the middle of the screen.
- A gun that doesn't get obscured by objects the player would use as cover.
I hope this was helpful! :D
hey zylay i have add a layer in the upperright next to layout i think thats what you mean. end i have get my gun in to this layer i have setup a new camera but my weapon stilll goes treu everyting is thare a way that i can do this with more explained i know what your trying to tell me bud i dont know unity so well am djust a starter 3ds max is my ground but tanks for your time anyway
Awesome, works great, I couldn't have imagined it would be that simple but it makes perfect sense when you explained it. If only I could upvote!
A little addition: This does not work out of the box when the rendering path is set to deferred rendering - just force forward rendering for the second camera.
Unfortunately this doesn't work great if using effects using depth buffer like AO and edge detection, as the second rendering will clear the depth buffer. Ideally UT would supply the ability to clear the Z buffer, but not the depth buffer.
so when I add the layer to gun it brings up a window thats says " do you want to make child for all? or just this object" when I click make all it makes a double of the gun, but when I tried it for just the object it doesnt work, still goes through walls. Ive noticed that when I selected make all child and it doubles the weapon, that one of the doubles actually work but the other doesnt.
Answer by Clunk · Sep 17, 2011 at 07:12 PM
Add a new camera, put it in the same position as the main camera, and make it a child of the main camera, so it always looks in the same direction.
Select your gun, add a new layer called ("Gun") or anything you wanna name it. Then with your gun selected, change it to that new layer.
Go to the new camera's properties, and under Culling Mask options, first, select "None" to clear. Then again, under Culling Mask Options, select "Gun", or whatever you named your layer.
Under Clear Flags, select "Depth Only"
Now for Depth, set to "1".
Set the near clip plane to 0.01, and far to 5, or however far out your gun's object goes.
Now your gun should not go through walls or any other objects. Any problems, email me and I will do this step by step with you to make sure you get it working :) Clunk47@Gmail.com
Answer by Peter G · Jan 04, 2011 at 09:44 PM
Please search for this next time you have this problem. It has been answered many times before.
Put your gun in a separate layer than everything else. Create a second camera that has its culling mask set to only draw that layer and have the clear flags to depth only.
Then have your main camera underneath it.
And there you have it. You gun will always be drawn on top of everything else.
Answer by Justin Warner · Jan 04, 2011 at 10:42 PM
I always just added a collider on to the gun... It's always worked for me...
And Yes, please www.google.com your answers, also, here's a custom search for Unity with Google...
http://www.google.com/cse/m?cx=002470491425767499270:iugs1ezlsfq&cref=&theme=DEFAULT
All the best.
Answer by Wesley S · Jan 09, 2011 at 01:55 AM
hey zylay i have add a layer in the upperright next to layout i think thats what you mean. end i have get my gun in to this layer i have setup a new camera but my weapon stilll goes treu everyting is thare a way that i can do this with more explained i know what your trying to tell me bud i dont know unity so well am djust a starter 3ds max is my ground but tanks for your time anyway
Follow this Question
Related Questions
damage system 0 Answers
How to Implement First Shot Accuracy? 2 Answers
Fps game gun reloading problem 1 Answer
Gun still clips through the wall despite layer masks being applied 1 Answer
Any problem with using real weapons in a FPS game? 0 Answers