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);
        //}
        ////Debug.Log("BOOM");
        ////Destroy(gameObject);
        if(collision.gameObject.tag != "BulletIgnore" && collision.gameObject.tag != "Cloud")
        {
            //Debug.Log("BOOM");
            Destroy(gameObject);
        }
    }
}

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

  • What colliders are you using for the structure and/or for the projectile? – Alakanu Sep 26 at 14:18
  • Do enemies have a collider as well as the barns? – Alakanu Sep 26 at 15:00
  • @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 – Demonic218 Sep 26 at 16:51
up vote 4 down vote accepted
+50

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) {
      Destroy(this);
      return;
    }
    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;
          break;
        }
      }
      if (!found) {
        Physics2D.IgnoreCollision(collider, overlapping[i], false);
        if (--overlappingCount == 0) {
          Destroy(this);
        } else {
          overlapping[i] = overlapping[overlappingCount];
        }
      }
    }
  }
}
  • Getting a Argument1: cannot convert from null to ContactFilter2d on the collider.OverlapCollider(null, testOutput); and collider.OverlapCollider(null, overLapping); – Demonic218 Sep 19 at 17:45
  • Ah, apparently ContactFilter2D is a struct. Try default(ContactFilter2d) instead of null. I've updated my answer. – Ed Marty Sep 19 at 18:06
  • 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. – Demonic218 Sep 19 at 18:23
  • Did you add this component to the bullet prefab, I assume? What happens? Have you tried debugging it at all? – Ed Marty Sep 24 at 14:45
  • 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. – Demonic218 Sep 25 at 16:26

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.