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);
}
}
}