- Home /
How do I make bullets pass through some game objects?
I am making a shmup and my player(space ship) shoots alien space ships but my player also has to dodge Asteroids... how do I make my bullets pass through the asteroids? I have the collision sorted out , so if I fly into the asteroid I die but now if I want to shoot an alien space ship behind the asteroid I can't because the asteroid acts as a shield. I have a bullet prefab and a Asteroid prefab both with rigid bodies and colliders.
Answer by TreyH · Sep 28, 2018 at 02:14 PM
There's an entire system dedicated to this already built into the physics engine. You're looking for Layer-based Collision Detection
Going into your physics settings, you should see a little matrix:
You'll add layers to your project and control who can hit what using this editor.
So should I give my bullet and my asteroid different layers to the rest of my game objects? And then change their layer collision so they don't collide? Do I need to implement any code?
Yeah, so you'll probably end up having different physics layers for each kind of object (player, enemy, environment, platforms, powerups, etc). If two layers aren't able to collide, then they will pass through each other.
You can also check which layer an object used during collision, if they can collide. Additionally, you can add a layer bitmask to physics ray/sphere/etc-casts to control which objects cause valid collisions with that call.