2 Lizards killed per projectile hit

The cactus idle and attacking seems to detect when a Lizard is in the same lane, but every time it fires, the Lizard in the same lane got hit and died (which is intended), but somehow the Lizard at the lane above also dies. Why?

I think my earlier tests before this segment of the course has a one hit one death instead of two. I added a second projectile which is supposed to fire some time after the first. I’ve also tried to remove this second firing but still the first firing will kill 2 lizards instead of one.

Here’s my Projectile.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour
{
[SerializeField] float speed = 1f;
[SerializeField] float damage = 50;

// Update is called once per frame
void Update()
{
    transform.Translate(Vector2.right * speed * Time.deltaTime);
}

private void OnTriggerEnter2D(Collider2D otherCollider)
{
    
    /*
    Debug.Log("I hit " + otherCollider.name);   // This is to check if collision works before implementing other stuff
    */

    var health = otherCollider.GetComponent<Health>();
    var attacker = otherCollider.GetComponent<Attacker>();

    if (attacker && health)
    {
        //reduce health
        health.DealDamage(damage);
        Destroy(gameObject);
    }

}

}


Here’s my Health.cs script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour
{
[SerializeField] float speed = 1f;
[SerializeField] float damage = 50;

// Update is called once per frame
void Update()
{
    transform.Translate(Vector2.right * speed * Time.deltaTime);
}

private void OnTriggerEnter2D(Collider2D otherCollider)
{
    
    /*
    Debug.Log("I hit " + otherCollider.name);   // This is to check if collision works before implementing other stuff
    */

    var health = otherCollider.GetComponent<Health>();
    var attacker = otherCollider.GetComponent<Attacker>();

    if (attacker && health)
    {
        //reduce health
        health.DealDamage(damage);
        Destroy(gameObject);
    }

}

}

I found my answer: The Box Collider for the zucchini is too large if I just box it around my vegetable. I reduce the edges to well within the inside of the object and now it’s one hit one kill.

Reason is I believe to place the zucchini at the right spot from the arm of the cactus, it will fly across two lanes. Since during the test all lizards are spawned at the same time, the top lizard will get hit too. It won’t be apparent that two lizards die per hit if they are spawned apart and the top one did not happen to cross path with the flying projectile in the row below.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.