Really strange collision issue

ok i’ve got four separate scripts that use collisions, but within those some work and some just dont

1.Projectile behaviour (doesnt do anything apart from making it so that the player can heal, stopped working?)

public void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.CompareTag("Enemy"))
        {
            EnemyHit = true;

            PE.PlantEnemyHealth -= BaseDamage; //* PS;
            WE.WeakEnemyHealth -= BaseDamage; //* PS;
            SE.StrongEnemyHealth -= BaseDamage; //* PS;
            EnemyHit = false;
        }

        if (collision.CompareTag("Healer"))
        {
            Destroy(this.gameObject);
        }

        else
        {

        }
       

    }

2.the start of my garbage dumpster fire code i’m so sorry
this makes it so the enemy attacks disappear when they hit the player or specific player projectiles, partially works

public void OnCollisionEnter2D(Collision2D Collision)
    {


        if (Collision.gameObject.CompareTag("Healer"))
        {
            Destroy(this.gameObject);
        }

        if (Collision.gameObject.CompareTag("Player"))
        {
            Destroy(this.gameObject);
        }

        if (Collision.gameObject.CompareTag("Projectile1"))
        {
            Destroy(this.gameObject);
        }

        if (Collision.gameObject.CompareTag("Projectile13"))
        {
            Destroy(this.gameObject);
        }

        if (Collision.gameObject.CompareTag("Projectile14"))
        {
            Destroy(this.gameObject);
        }

        if (Collision.gameObject.CompareTag("Projectile15"))
        {
            Destroy(this.gameObject);
        }

        if (Collision.gameObject.CompareTag("Projectile8"))
        {
            Destroy(this.gameObject);
        }


        }

3.this would be so much worse but i havn’t got past testing yet, it’s for a school project so it doesnt need to be uber polished lol (player gets damaged here obvs, no work)

    public void OnCollisionEnter2D(Collision2D Collider)
    {
        if (Collider.gameObject.CompareTag("EnemyProjectile1"))
        {
            PlayerHealth -= 20;

        }

    }
  1. im sure your sick of this so i’ll only put a few here, they all work in this script anyway
    (enemy taking damage from player works 100%)
 public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Projectile1"))
        {
            StrongEnemyHealth -= 25 ;
            StrongAnimation.SetTrigger("StrongHit");

            if (StrongEnemyHealth > -1 && StrongEnemyHealth < 1)
            {
                StrongEnemyHealth = 1;
            }
 

        }

        if (collision.CompareTag("Projectile2"))
        {
            StrongEnemyHealth -= 20;
            StrongAnimation.SetTrigger("StrongHit");
           // FreezeCheck += 1;

            if (StrongEnemyHealth > -1 && StrongEnemyHealth < 1)
            {
                StrongEnemyHealth = 1;
            }

        }
       
        if (collision.CompareTag("Projectile3"))
        {
            StrongEnemyHealth -= 20;
            StrongAnimation.SetTrigger("StrongHit");

            if (StrongEnemyHealth > -1 && StrongEnemyHealth < 1)
            {
                StrongEnemyHealth = 1;
            }

        }

the main blaring issue here is that one, i can’t code, and two for some reason in these scripts parts just stop registering as colliding.
the worst part is that at one point they were all working fine its just gradually become broken, and i genuingly have no idea why.

specifically in 2 (EnemyProjectileBehaviour) the enemies attacks just phase through the player, but when hitting the projectiles in the same collision script it works fine.

another bit that just stopped is in the first script(ProjectileBehaviour) the enemies take damage now but the healer system just doesn’t work at all. phases through like the enemy attacks to the player.

iv’e gone to the lengths of making the enemies, player/healer, projectiles and enemyProjectiles all on different layers which seem to sometimes work but like… doesnt in cases, i knw it’s not the problem as results are the same before and after implementing this.

if you need any more information just lmk, genuinely dumfounded and kind of mad no clue whats going on right now

The first thing that stands out to me is that some of these scripts use OnTriggerEnter and others use OnCollisionEnter. This makes me believe that you have some problems with your physics components and how they are setup. In particular, OnCollision events will change whether or not they fire based on how the rigidbodies attached to each object are configured. For the most part, using OnTrigger events is better when you can use them (which should be most of the time). Take a look at the event matrix near the bottom of this page: Unity - Manual: Introduction to collision
That page is a great reference for the physics system and how to correctly configure rigidbodies so that the different physics events trigger as expected.
Some general tips:

  1. Avoid mesh colliders. They don’t collide with each other. There are ways you can use them, but as a new dev, I recommend just avoiding the headache.
  2. Do not move rigidbodies by changing their transform’s position. This will cause physics to behave unexpectedly and unrealistically. This does not apply to rigidbodies that are marked kinematic. Kinematic rigidbodies are intended to be moved through their transform, other rigidbodies are not.
  3. Make sure at least one object involved in a collision or trigger event has a rigidbody attached to it. Ideally, give everything a rigidbody and mark the ones you want to control through script as kinematic.
  4. Use trigger liberally. Triggers are love, triggers are life.
    EDIT:
  5. Note in the collision action matrix, two kinematic rigidbodies DO NOT fire collision events when they contact each other. They do fire trigger events.
1 Like