Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by hillsy7 · Nov 21, 2017 at 03:15 PM · collisionraycasttrigger

Collision physics with some objects, not others

Hi Guys,

I’ve just been trying to work out how to have my Player bang into things (Rigidbody physics) but not be knocked about when hit with a projectile (OnTriggerEnter). A quick scoot through various forums and whatnots shows there’s quite a lot of people asking the question and there’s a real range of answer, all based on differing requirements of the outcomes – and it’s a little bewildering. In the end I actually created a tiny raycast that destroys the object before collision (Stopping the collision taking effect).

Is this the right way to do it? Should I be using a more elegant solution? I’m new to Unity and so I don’t know what’s working because I’m creating a huge, inelegant workaround, and what works because it’s a good way of doing it.

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 Igor_Vasiak · Nov 21, 2017 at 05:27 PM 0
Share

Have you tried layer collision?

Let's say you don't want the player to collide with your bullets at all.


The easiest way to do this is by making your bullet a trigger collider. For this you just need to mark the little trigger box inside the Collider that you're using on your bullet (that's also needed for using OnTriggerEnter()). But... That's only the easy way...


The complicated way (and the most complete, by far) is the layer collision method.

First of all, select an Object on your scene. Any Object. You'll go to the top right corner of the Inspector Tab and select Layer. Then, select Add Layer... .

Now, type at one of the empty boxes, Player, and at another one, Bullets.

Then select your player and assign to it the Player layer. Select your bullet's prefab and assign to it the layer Bullets.


Now, go to Edit > Project Settings > Physics (or Physics2D, depending on which are you using). On the new tab that opened inside Inspector you'll notice, there is an upside down staircase, made out of Toggles(check boxes). You'll also notice that each line and each column have their own names.

It's like on a chess game. You have A1, A2, A3... All the way up to H8. It's the same thing here. Each toggle makes a crossing line with each other. So, you'll have a toggle that comes from line Player and from the column Bullets. If it's true, it means that everything inside the layer Player CAN interact (collide, trigger, etc) with everything inside the layer Bullets. Set it to false, and the big CAN some words ago will become CAN'T.

Now you can test it.


By the way, if anybody knows other solutions, or if I've made any mistakes explaining this, let me know. Or even re-explain what I wrote wrong. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MastersOfUs · Nov 21, 2017 at 05:44 PM

First, assign different layers to the objects you do not want to collide with each other. You do this by selecting your object and using this menu in the inspector:

You can create new layers, as the default ones may not be enough. Now, go to Edit > Project Settings > Physics. At the bottom of the physics settings, you have the "Layer Collision Matrix". You can use the checkboxes to determine which layers collide with other layers.

Looking at your question, however, it looks like you may only need to check "Is Trigger" on the colliders of your bullets. This will make them not collide with anything, and only call "OnTriggerEnter" when they intersect another collider.

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 hillsy7 · Nov 21, 2017 at 10:01 PM 0
Share

Yeah, I tried the layers and I couldn't get the trigger colliders to fire while ignoring the physics.

Just this evening though I found part of the problem. I initially just went with the trigger switched on, and it didn't work. However, I rebuilt the whole thing back up and discovered the trigger does actually work, but only if the ship isn't moving. As soon as the collider with the rigid body is moving, it registers a collison.

Weirdly it only works one way, which is why I went for the raycast approach....

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.

Follow this Question

Answers Answers and Comments

147 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 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 raycasts and triggers 1 Answer

Help with collision detection with specific colliders 1 Answer

How to check if raycast is hitting object's child's collision box? 3 Answers

Is it possible to check how much a trigger is filled by %? 1 Answer

How to Find Owner of a Trigger? 1 Answer


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