Home > other >  Why is this Projectile Script Continuing to Hit Targets After I've Destroyed It?
Why is this Projectile Script Continuing to Hit Targets After I've Destroyed It?

Time:07-30

I am new to Unity2D. I am trying to make castle defense game. When the spawners start to inheritance the enemies overlap (they should), but when the archer arrow collides whit the enemies it kills them all. I searched everywhere for the answer of this but nothing... My questions is: Is there a way to only hit one target at time?

Arrow script:

void Start()
{
    target = GameObject.FindGameObjectWithTag("Enemy").GetComponent<Transform>();
    
}

// Update is called once per frame
void Update()
{
    
    transform.position = Vector2.MoveTowards(transform.position, target.position, speedProjectile * Time.deltaTime);
}

private void OnTriggerEnter2D(Collider2D collision)
{ 
    if (collision.gameObject.CompareTag("Enemy"))
    {
        Destroy(gameObject);
    }
    
}

Enemy script:

 void Start()
{
    
    target = GameObject.FindGameObjectWithTag("target3").GetComponent<Transform>();
}

// Update is called once per frame
void Update()
{
    
    transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);

}
private void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.CompareTag("arrow"))
    {  
        EHealth -= HDamage;
    }
    if (EHealth <= 0) 
    {
        Destroy(gameObject);
    }

CodePudding user response:

its hard to say with the information given, based on what you have said it seems that all the entity's are just 1 entity(so when you kill 1 enemy you kill the only enemy which is all of them). you can have them run independently from each other.

CodePudding user response:

Maybe you can use collision enter function to check bullet is hitting enemy's body. You can use this code below...

private bool isEntered = false;
void OnCollisionEnter(Collision collision)
{
    if(isEntered) return;
    if(collision.gameObject.tag == "enemy") isEntered = true;
    ....
    ....
}

I hope it will work for you.

CodePudding user response:

Your enemies are all taking damage because destroying a GameObject isn't something that happens immediately (for good reason). Because of this, in a single frame, any number of enemies can be hit by the same arrow.

If you'd like to rely on these collision methods, I'd suggest controlling the damage from the arrow instead of from the enemy, so you can be sure it only hits something once:

private bool dealtDamage = false;

private void OnTriggerEnter2D(Collider2D col) {  
    // Only deal damage once
    if (dealtDamage) { 
        return; 
    }

    // Does the thing I hit have this "EnemyScript" ?
    var enemy = col.gameObject.GetComponentInChildren<EnemyScript>();
    if (enemy == null) {
        return;
    }
     
    // Make this "DealDamage()" method public on your EnemyScript
    enemy.DealDamage();
    dealtDamage = true;
    Destroy(gameObject);
}

Then get rid of the Enemy Script's OnTriggerEnter2D because the arrow is handling it all. I don't know what the name of the script is on your enemies so I just called it EnemyScript. This is also calling a DealDamage() method that you'd have to make (which would probably look a lot like the one you current have listed in your "Enemy Script.")

  • Related