- Home /
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.
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. :)
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.
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....