• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by coolbudy1998 · Jul 25, 2018 at 03:36 PM · rigidbodycolliders

Weapon pass through objects

Hey everyone, my gun passes through a cube. How can I make it collide with it? I have added box collider and rigidbody to both of them still it doesn't work. "Is trigger", "Is kinematics" and "Gravity" are set to false for both objects. Help!!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RLin · Jul 25, 2018 at 03:48 PM 0
Share

Please post code used to move the objects and possibly post pictures of your setup in the editor. It’s impossible to tell what is wrong if we can’t see what you are currently doing.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Zarenityx · Jul 29, 2018 at 01:48 PM

I assume you're making an FPS or something similar? A lot of FPS games don't even bother with checking collision between weapons and the environment. I'm not sure exactly what your needs are, but there are a couple of options:

Option 1: Check Collisions
If an object is being moved via its transform (either with transform.position or by being the child of a moving object like your character), it won't respond to physics, because it is being explicitly told where to go. However, the collider will still be sending collision messages. I would suggest making the collider a trigger to prevent strange interactions with physics that might feel strange to the player. Then, in a script, do something where

 void OnTriggerEnter(Collider col){
      if(col.tag !- "Player"){
           //Get the gun out of the way via animation or something
      }
 }

What you do to get the gun out of the way is up to you, but I imagine an animation or a lerp to a position could work. Keep in mind that the collider actually makes more sense to be spherical here, since it extends down as much as it does forward, and the side extensions will actually feel more natural and anticipatory to the player. You also might (and probably will) want to also do the next technique

Option 2: Render in Front
This is what almost every FPS does, and it works very well. I highly recommend you also implement this method.
In most FPS games, players' weapons actually still do go through the environment, it just doesn't look like it because they are rendered on top of the environment. This is done by assigning the gun to a separate layer, say, "Weapons," and creating a second camera. The main camera that you use to see the environment should have its culling mask modified to exclude the "Weapons" layer. Then, a second camera should be parented to the main camera and look from the exact same spot, but have its culling mask only include the "Weapons" layer. Additionally, the second camera's depth should be lower than the main camera's depth, and the second camera's clear flags should be set to "Depth Only."

This will cause the gun to be rendered on top of the rest of the environment, so no matter how far into the environment it actually is. While this is physically wrong, it looks and feels right to the player, because people don't just stop walking when their gun would touch something.

Option 2.5: Both
Both of these options can be done in tandem. Personally, I feel that Option 2 gives the biggest impact on how the game feels, and is also always guaranteed to work, but Option 1 is rather neat to see, and can help cover up some of the shortcomings of Option 2 when dealing with long guns that can sometimes appear a little too short when close to walls. Option 1 and 2 also work together quite nicely in that Option 1 provides a nice little animation that adds some depth to the motion on the screen, and Option 2 gives you the freedom to make these animations look nice, rather than requiring that they actually get the gun out of the way, which could end up with the gun being off the screen or in an awkward corner of it.

These techniques can be applied to things like player arms, hands, etc. as well. Good luck on your project!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbudy1998 · Jul 29, 2018 at 03:00 PM 0
Share

Thanks!! I am grabbing it.

avatar image Zarenityx coolbudy1998 · Jul 31, 2018 at 08:25 PM 0
Share

Did it work for you? (If so could you accept my answer?)

avatar image
0

Answer by Grundadrakk · Jul 29, 2018 at 12:55 PM

Is it always passing through the cube?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zarenityx · Jul 29, 2018 at 01:50 PM 0
Share

This really should be posted as a comment, not an answer. Answers should attempt to solve a question, while comments can be used to ask for clarification.

avatar image coolbudy1998 · Jul 29, 2018 at 02:40 PM 0
Share

yes always!! Colliders and rigidbody aren't acting on that!

avatar image
0

Answer by AidanWalsh4 · Jul 25, 2018 at 05:09 PM

How are you moving these objects? If you are using something like transform.translate or transform.position, colliders can intersect with each other.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbudy1998 · Jul 29, 2018 at 12:04 PM 0
Share

Hi, my gun is a child of pre-formed FPS controller. And so it moves with the pre-scripted scripts.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a capsule collider is giving me some trouble with collisions (pics inside) 0 Answers

Child colliders affecting parent rigidbody characteristics? 0 Answers

Why does my player fly through my barrier? 2 Answers

Player with RB + capsule collider can easily penetrate ALL kinds of primitive colliders. What do I do to fix this? 2 Answers

Compound colliders, what is the best implementation? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges