object not colliding with invisible object

I have two trap which are the same:

  • a invisible trap that is on the enemy (child of the enemy), has a tag of ‘Ability’ (has RB2d, box collider but turned off)
  • a trap ability so when the player hits the ability button, it shoot a trap prefab, has a tag of ‘Ability2’ (has rb2d and box collider turned on)

What I want:

  • when trap ability is shot and collides with the invisible trap, spriteRenderer turns on (makes trap visible), box collider turns on, so when player shoots a projectile and it collides with the trap, enemy takes damage, spriteRenderer turns off (trap is invisible) and box collider turns off

What actually happens:

  • when trap ability is shot, it ignores the invisible trap and hits and pushes the enemy which i dont want

invisible trap script:

public Enemy enemy;

public GameObject lightningTrap;

public Mouse refToShotCount;

private void Start()
{
    refToShotCount = GameObject.Find("Mouse").GetComponent<Mouse>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
    if(collision.gameObject.tag == "Ability2")
    {
        print("collided");
        lightningTrap.GetComponent<SpriteRenderer>().enabled = true;
        lightningTrap.GetComponent<BoxCollider2D>().enabled = true;
        Destroy(GameObject.FindGameObjectWithTag("Ability2"));
    }

    if (collision.gameObject.tag == "Projectile")
    {
        refToShotCount.shotCount++;
        enemy.randomDamage = Random.Range(50, 76);
        enemy.TakeDamage(enemy.randomDamage);

        enemy.ShowDamageNumber();
        lightningTrap.GetComponent<SpriteRenderer>().enabled = false;
        lightningTrap.GetComponent<BoxCollider2D>().enabled = false;
        Destroy(GameObject.FindGameObjectWithTag("Projectile"));
    }
}

1 Answer

1

Actually the issue you’re encountering is that the Ability2 trap is colliding with the enemy itself, rather than the invisible Ability trap that is a child of the enemy. This is happening because the Ability2 trap has a rigidbody and collider attached, which are causing it to collide with other objects in the scene.

To fix this issue, you will need to disable the rigidbody and collider on the Ability2 trap, so that it does not collide with other objects in the scene. You can do this by unchecking the “Is Kinematic” and “Used By Composite” options on the rigidbody, and by unchecking the “Is Trigger” option on the collider. This will prevent the Ability2 trap from colliding with other objects, and will allow it to pass through the invisible Ability trap and trigger the collision event as intended.e.g
public class Ability2Trap : MonoBehaviour
{
// Other code omitted for brevity

    private void Start()
    {
        // Get the rigidbody and collider components on the trap
        var rigidbody = GetComponent<Rigidbody2D>();
        var collider = GetComponent<BoxCollider2D>();

        // Disable the rigidbody and collider
        rigidbody.isKinematic = true;
        rigidbody.usedByComposite = false;
        collider.isTrigger = true;
    }
}

Yo welcome :slight_smile:

Thank You, it worked! usedByComposite gives me an error but I commented it out and it still worked