Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How do I make the gun not go though the terrain and objects?

Discussion in 'Editor & General Support' started by General Jackson, Mar 10, 2010.

  1. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    I have a FPS gun setup, but there is no collision for it with terrain objects.
    How do i fix this?
     
  2. Jehsup

    Jehsup

    Joined:
    May 22, 2008
    Posts:
    166
    Add a collider to it. Box or cylinder should work nicely depending on your guns shape.
     
  3. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    No that wont work I attached a pic of what its doing.
    The manual says how to fix it but its not clear enough and i dont understand :(

    See how it goes right through the terrain?
     
  4. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    If it goes thru the terrain, it'll probably go thru walls as well.

    I know alot of games use a raycast to determine if the model will poke thru something, if so, it'll play an animation of the gun being pulled back, or pointed up or something like that.

    Basically if your gun sticks out 1 unit, and you are less then one unit away, it will poke thru, so you'd need to play the animation to adjust the gun.

    Alternatively, there's the option of rendering the gun above everything else via layers. I think this is what the FPS tutorial tells you to do.
     
  5. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
  6. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    Could you tell me exatly how to do the depth-buffer? Like I already said, I tried what the manual said but it gave so little info I dont understand it :(
     
  7. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    800
    -Create a new layer for your weapons. Set all your weapon's layers to this new layer.

    -Now create a second camera, set it to render only the new Layer, your weapon's layer. Set it's ClearFlags to Depth Only.

    -Set your main camera to not render the weapon's layer.

    -Give it a higher depth value then the main camera.

    If you do all these correctly, it will solve your problem. There is no need for any scripts, these are mostly done in your camera object settings in the inspector window.
     
  8. coin-god

    coin-god

    Joined:
    Jan 21, 2010
    Posts:
    325
    Do this, it works for me.
     
  9. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    It really works? perhaps I will try this. I tried the raise gun thing, but getting everything to work right is hard. And using box coulliders on simple map parts works, but not on meshes. So im gonna give this a try! :D
     
  10. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    HOLY F**K! IT WORKS! That is truly amazing! Thanks a lot! :D :D :D :D :D :D :D :D :roll:
     
  11. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    800
    You are welcome, it's the technique used in almost all the FPS games out in the market today.
     
  12. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    I suppose so. Back in the pre-2000's, the gun was simply an animated part of the GUI, not a real object.
     
  13. JTshoot

    JTshoot

    Guest

    Joined:
    Mar 17, 2010
    Posts:
    39
    How do i create a layer ?
     
  14. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Go to the top of the inspector, click the drop-down that says "Layer", and click "Add New Layer".
     
unityunity