Enemy laser not showing up in scene when its CapsuleCollider2D has isTrigger

Hi all, I have this strange problem with making the enemy shoot lasers.

When I tick my enemy’s laser CapsuleCollider2D’s ‘isTrigger’, the enemy lasers stop instantiating in the game. The strange thing is that when I start a debugging session and do Debug.Log(), it looks like the laser is instantiating and its Ridgidbody2D has velocity set correctly.
When I hit play, and switch the ‘isTrigger’ off during playing, the lasers start appearing in the scene…

I did all the required steps, copied the code carefully, did a lot of debugging but still, I haven’t found a solution to the issue.

Help please :slight_smile:

It’s hard for us to debug without seeing the code. If I had to guess the collider is hitting the enemy ships collider and destroying itself immediately after instantiating. That is just a guess. Is there any OnTriggerEnter or the like code?

My best guess is like the previous reply, that the bullets are being instantiated and then immediately hit the enemy and gets destroyed.
How to fix this:

  • in the inspector open the “layer” drop down menu
  • add new layers to your scene (e.g Player, Player Projectile, Enemy, Enemy Projectiles etc)
  • find the “layer collision matrix” in the project settings and you can check the boxes for what game objects you want to collide with each other (player can collide with enemy and enemy projectile but not player projectile; enemy can collide with enemy only, enemy projectile can collide with player only)

Hope this helps :smiley:

Yes, looks like that is exactly what was happening!
I made a quick test by disabling the code that destroys the laser upon hit, and all lasers started appearing in the game.

I am moving to the ‘layer collision matrix’ lesson and hope this will get fixed as you are saying @Jesse_W :slight_smile:

Thank you guys, it’s impossible how quick the community is :smiley:

@TWasilonek, I’m glad you found a potential solution. Did it fix the problem for you? :slight_smile:


See also:

Yes, I marked the correct comment as solution

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms