I’m learning to make a 2D platformer. I have a gun rotating around the mouse pointer and shooting at the direction of the mouse pointer when the left mouse button is clicked. The flying enemy has a Box Collider 2D on it. When i use the gun to kill the original flying enemy, it worked pretty well, the flying enemy died. But after i created a duplicate of my original flying enemy, something weird happened. If i shoot at the duplicate one, it would die like normal. But if i shoot the original one first, the duplicate one will be taken damage and will die first then if i keep shooting at it, the original one will be taken damage and die like normal. Can someone point out my problem here? Thank you for taking your time to read my question.
Here is the script i attached to the gun.
void FireRocket()
{
Vector2 mouPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 gunTipPosition = new Vector2(gunTip.position.x, gunTip.position.y);
RaycastHit2D hit = Physics2D.Raycast(gunTipPosition, mouPos - gunTipPosition);
if (hit.collider != null)
{
if(hit.collider.tag=="Flying Enemies")
{
FindObjectOfType<MakeDead>().RemoveHealth(damage);
}
}
}
And here is the scrip i attached to the flying enemy
void Start () {
currentHp = maxHp;
}
public void RemoveHealth(float amount)
{
currentHp -= amount;
healthBar.fillAmount = currentHp / maxHp;
if (currentHp <= 0 && !hasDied)
{
hasDied = true;
Destroy(gameObject);
Instantiate(bloodParticle, transform.position, transform.rotation);
}
}