Enemy damage is tripled.

I am creating a 2D dungeon crawler and just started working on ranged enemies that use the same bullet prefab as the player does. For some reason, whenever the enemy shoots the player, Player will take triple the damage than he should. Even though DamagePlayer is set to 1, he will lose 3 health.

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

public class GameController : MonoBehaviour
{

    public static GameController instance;

    private static float health = 3;
    private static float maxHealth = 3;
    private static float moveSpeed = 15f;

    public static float Health { get => health; set => health = value; }
    public static float MaxHealth { get => maxHealth; set => maxHealth = value; }
    public static float MoveSpeed { get => moveSpeed; set => moveSpeed = value; }

    public Text healthText;

    // Start is called before the first frame update
    void Awake()
    {
        if(instance == null)
        {
            instance = this;
        }
    }

    // Update is called once per frame
    void Update()
    {
        healthText.text = "Health: " + health + "/" + maxHealth;
    }

    public static void DamagePlayer(int damage)
    {
        health -= damage;

        if(Health <= 0)
        {
            KillPlayer();
        }
    }

    public static void HealPlayer(float healAmount)
    {
        health = Mathf.Min(maxHealth, health + healAmount);
    }

    public static void MoveSpeedChange(float speed)
    {
        moveSpeed += speed;
    }

    public static void MaxHealthChange(float maxhp)
    {
        maxHealth += maxhp;
    }

    private static void KillPlayer()
    {

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

public enum BossState
{
    Idle,
    Follow,
    Die,
    Attack
};

public enum BossType
{
    Melee,
    Ranged
};

public class BossController : MonoBehaviour
{

    GameObject player;
    public BossState currState = BossState.Idle;
    public BossType bossType;
    public float range;
    public float speed;
    public float attackRange;
    public float coolDown;
    private bool chooseDir = false;
    private bool dead = false;
    private bool coolDownAttack = false;
    public bool notInRoom = false;
    public GameObject bulletPrefab;
    private Vector3 randomDir;
    Rigidbody2D rb2D;
    public Transform bossFirePoint;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        rb2D = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        switch (currState)
        {
            case (BossState.Idle):
                Idle();
                break;
            case (BossState.Follow):
                Follow();
                break;
            case (BossState.Die):
                break;
            case (BossState.Attack):
                Attack();
                break;
        }
        if (!notInRoom)
        {
            if (IsPlayerInRange(range) && currState != BossState.Die)
            {
                currState = BossState.Follow;
            }
            else if (!IsPlayerInRange(range) && currState != BossState.Die)
            {
                currState = BossState.Idle;
            }
            if (Vector3.Distance(transform.position, player.transform.position) <= attackRange)
            {
                currState = BossState.Attack;
            }

        }
        else
        {
            currState = BossState.Idle;
        }
    }

    private bool IsPlayerInRange(float range)
    {
        return Vector3.Distance(transform.position, player.transform.position) <= range;
    }

    private IEnumerator ChooseDirection()
    {
        chooseDir = true;
        yield return new WaitForSeconds(Random.Range(2f, 8f));
        randomDir = new Vector3(0, 0, Random.Range(0, 360));
        Quaternion nextRotation = Quaternion.Euler(randomDir);
        transform.rotation = Quaternion.Lerp(transform.rotation, nextRotation, Random.Range(0.5f, 2.5f));
        chooseDir = false;
    }

    void Idle()
    {
        if (IsPlayerInRange(range))
        {
            currState = BossState.Follow;
        }
    }

    void Follow()
    {
        transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
    }

    void Attack()
    {

        if (!coolDownAttack)
        {
            switch (bossType)
            {
                case (BossType.Melee):
                    GameController.DamagePlayer(1);
                    StartCoroutine(CoolDown());
                    break;
                case (BossType.Ranged):
                    GameObject bullet = Instantiate(bulletPrefab, bossFirePoint.position, bossFirePoint.rotation);
                    bullet.GetComponent<Bullet>().GetPlayer(player.transform);
                    bullet.GetComponent<Bullet>().isEnemyBullet = true;
                    StartCoroutine(CoolDown());
                    break;
            }
        }
    }

    private IEnumerator CoolDown()
    {
        coolDownAttack = true;
        yield return new WaitForSeconds(coolDown);
        coolDownAttack = false;
    }

    public void Death()
    {
        Destroy(gameObject);
    }
}

First step would be to add some Debug.Log statements around your code. For example. put one inside the DamagePlayer() to make sure it’s being called the right number of times.

Aside from that - the issue is probably in the Bullet script, which you haven’t shared here.

1 Like

Debug.Log says that 1 damage was taken, but it pops up three times every time you get hit. Only one bullet is spawning though.

here is my bullet script

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

public class Bullet : MonoBehaviour
{

    public GameObject hitEffect;
    public bool isEnemyBullet = false;

    private Vector2 lastPos;
    private Vector2 curPos;
    private Vector2 playerPos;

    void OnCollisionEnter2D(Collision2D collision)
    {
        GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
        Destroy(effect, 1f);
        Destroy(gameObject);
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if(col.tag == "Enemy")
        {
            col.gameObject.GetComponent<EnemyController>().Death();
            Destroy(gameObject);
        }

        if(col.tag == "Player" && isEnemyBullet)
        {
            GameController.DamagePlayer(1);
            Destroy(gameObject);
        }
    }



    void Update()
    {
        if (isEnemyBullet)
        {
            curPos = transform.position;
            transform.position = Vector2.MoveTowards(transform.position, playerPos, 5f * Time.deltaTime);
            if (curPos == lastPos)
            {
                Destroy(gameObject);
            }
            lastPos = curPos;
        }
    }

    public void GetPlayer(Transform player)
    {
        playerPos = player.position;
    }

}