In my game I have enemies that shoot projectiles out at the player, the only way for the player to get rid of them is by guiding them into a structure.

Enemy 1 shooting projectile

An example of my enemy shooting a projectile.

The problem I have is that sometimes the enemy spawns on top of one of these structures as seen below.

Problem Image

This results in the projectile exploding/colliding with the structure the moment it is spawned.

I'm not entirely sure how to go about fixing this issue.

I have tried to find a way to disable the collider on the projectile for a few seconds but I couldn't get the timer right. I also think this might cause some odd game-play issues.

The second thing I tried was a platform effector, however I could only get it to form a circle and this didn't work out because my structures are of all different sizes and shapes.

If anyone could point me into the right direction or even help me find a better solution I'd be extremely grateful. Thanks

ublic class MultiBullet : MonoBehaviour {

    public float speed = 1f;

    private Rigidbody2D rb;

    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody2D>();


    // Update is called once per frame
    void Update () {
        rb.velocity = transform.up * speed;

    private void OnTriggerEnter2D(Collider2D collision)
        ////Add explosion Effect
        //if (collision.gameObject.tag == "Player")
        //    // Add game over screen here
        //    Debug.Log("Player Killed");
        //    Destroy(gameObject);
        if(collision.gameObject.tag != "BulletIgnore" && collision.gameObject.tag != "Cloud")

Just a simple class to handle when projectiles collide with objects.

  • \$\begingroup\$ What colliders are you using for the structure and/or for the projectile? \$\endgroup\$
    – Alakanu
    Sep 26, 2018 at 14:18
  • \$\begingroup\$ Do enemies have a collider as well as the barns? \$\endgroup\$
    – Alakanu
    Sep 26, 2018 at 15:00
  • \$\begingroup\$ @Alakanu - The colliders are all of the 2D kind, pollygon, box, custom. Depends on the shape of the game object. Yes the enemies have colliders as well as the barn \$\endgroup\$
    – Demonic218
    Sep 26, 2018 at 16:51

1 Answer 1


Overview: immediately after instantiation, check what objects the projectile overlaps. Disable collisions between those objects. Then, on each frame, re-check that list and when it no longer overlaps a collider, reenable collisions between those objects.

Warning: untested

public class ProjectileOverlapCheck : MonoBehavior {
  private Collider2D[] overlapping;
  private Collider2D[] testOutput;
  private Collider2D collider;
  private int overlappingCount;

  private void Start() {
    int max = 4; //Note: if it could be overlapping more than 4 things on the first frame, increase this number
    overlapping = new Collider2D[max];
    testOutput = new Collider2D[max];
    collider = GetComponent<Collider2D>();
    ContactFilter2D filter = new ContactFilter2D().NoFilter();
    overlappingCount = collider.OverlapCollider(filter, overlapping);
    if (overlappingCount == 0) {
    for (int i = 0; i < overlappingCount; i++) {
      Physics2D.IgnoreCollision(collider, overlapping[i], true);

  private void FixedUpdate() {
    ContactFilter2D filter = new ContactFilter2D().NoFilter();
    int newOverlappingCount = collider.OverlapCollider(filter, testOutput);
    for (int i = overlappingCount - 1; i >= 0; i--) {
      bool found = false;
      for (int j = 0; j < newOverlappingCount; j++) {
        if (overlapping[i] == testOutput[j]) {
          found = true;
      if (!found) {
        Physics2D.IgnoreCollision(collider, overlapping[i], false);
        if (--overlappingCount == 0) {
        } else {
          overlapping[i] = overlapping[overlappingCount];
  • \$\begingroup\$ Getting a Argument1: cannot convert from null to ContactFilter2d on the collider.OverlapCollider(null, testOutput); and collider.OverlapCollider(null, overLapping); \$\endgroup\$
    – Demonic218
    Sep 19, 2018 at 17:45
  • \$\begingroup\$ Ah, apparently ContactFilter2D is a struct. Try default(ContactFilter2d) instead of null. I've updated my answer. \$\endgroup\$
    – Ed Marty
    Sep 19, 2018 at 18:06
  • \$\begingroup\$ That seemed to fix the null error but hasn't done anything for my original issue. I never combined this with my previous method on the projectile, could that be the reason ? Ill add it to my question so you can take a look. \$\endgroup\$
    – Demonic218
    Sep 19, 2018 at 18:23
  • \$\begingroup\$ Did you add this component to the bullet prefab, I assume? What happens? Have you tried debugging it at all? \$\endgroup\$
    – Ed Marty
    Sep 24, 2018 at 14:45
  • \$\begingroup\$ Greetings, Thanks for the reply. Yes I've added it to the bullet, along with the script that contains the onTrigger effect. So to be clear there are two scripts on the bullet. When it came to debugging, the overlappingCount was 0 resulting in the if statement being triggered. The overlapping array is all null. \$\endgroup\$
    – Demonic218
    Sep 25, 2018 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .