About is trigger checkbox in collider

in the lecture, instructor ask to check the is trigger checkbox in player laser prefab, why? why not check the is trigger checkbox in enemy prefab.

Hi,

It’s a matter of personal preference. Since we spawn more lasers than enemies, it makes sense to use trigger colliders, which are more performant than non-trigger colliders.

Has your question been answered?

not really, since the enemy script is associated with enemy gameobject, not with player laser object. inside the enemy script it has one method called ontriggerenter2d and parse a collider2d object, which means the enemy collider is the major one, the parameter (laser) is the other. however, the laser collider is the trigger, but there is no script for laser collider to execute. so my question is how the method ontriggerenter2d works, and which one should be the parameter

The laser has got a Collider2D component attached, doesn’t it? If so, that’s how it works. In Unity, when a collider collides with another collider, certain methods get executed. For example, if a trigger collider touches a non-trigger collider, the OnTriggerEnter2D method gets called in all components attached to both game objects involved in the collision. If the OnTriggerEnter2D method is “attached” to the enemy, the argument which gets passed on to OnTriggerEnter2D refers to the laser. And if the method is “attached” to the player, the argument refers to the enemy.

Feel free to create a simple test script and attach it to the player laser (and also the enemy if you want). In that class, you add the OnTriggerEnter2D method with a Debug.Log and a meaningful message.

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

Privacy & Terms