Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Enemy damage is tripled.

Discussion in 'Scripting' started by M3ntalll, Sep 4, 2020.

  1. M3ntalll

    M3ntalll

    Joined:
    Jul 1, 2020
    Posts:
    8
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameController : MonoBehaviour
    7. {
    8.  
    9.     public static GameController instance;
    10.  
    11.     private static float health = 3;
    12.     private static float maxHealth = 3;
    13.     private static float moveSpeed = 15f;
    14.  
    15.     public static float Health { get => health; set => health = value; }
    16.     public static float MaxHealth { get => maxHealth; set => maxHealth = value; }
    17.     public static float MoveSpeed { get => moveSpeed; set => moveSpeed = value; }
    18.  
    19.     public Text healthText;
    20.  
    21.     // Start is called before the first frame update
    22.     void Awake()
    23.     {
    24.         if(instance == null)
    25.         {
    26.             instance = this;
    27.         }
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         healthText.text = "Health: " + health + "/" + maxHealth;
    34.     }
    35.  
    36.     public static void DamagePlayer(int damage)
    37.     {
    38.         health -= damage;
    39.  
    40.         if(Health <= 0)
    41.         {
    42.             KillPlayer();
    43.         }
    44.     }
    45.  
    46.     public static void HealPlayer(float healAmount)
    47.     {
    48.         health = Mathf.Min(maxHealth, health + healAmount);
    49.     }
    50.  
    51.     public static void MoveSpeedChange(float speed)
    52.     {
    53.         moveSpeed += speed;
    54.     }
    55.  
    56.     public static void MaxHealthChange(float maxhp)
    57.     {
    58.         maxHealth += maxhp;
    59.     }
    60.  
    61.     private static void KillPlayer()
    62.     {
    63.  
    64.     }
    65. }
    66.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum BossState
    6. {
    7.     Idle,
    8.     Follow,
    9.     Die,
    10.     Attack
    11. };
    12.  
    13. public enum BossType
    14. {
    15.     Melee,
    16.     Ranged
    17. };
    18.  
    19. public class BossController : MonoBehaviour
    20. {
    21.  
    22.     GameObject player;
    23.     public BossState currState = BossState.Idle;
    24.     public BossType bossType;
    25.     public float range;
    26.     public float speed;
    27.     public float attackRange;
    28.     public float coolDown;
    29.     private bool chooseDir = false;
    30.     private bool dead = false;
    31.     private bool coolDownAttack = false;
    32.     public bool notInRoom = false;
    33.     public GameObject bulletPrefab;
    34.     private Vector3 randomDir;
    35.     Rigidbody2D rb2D;
    36.     public Transform bossFirePoint;
    37.  
    38.     // Start is called before the first frame update
    39.     void Start()
    40.     {
    41.         player = GameObject.FindGameObjectWithTag("Player");
    42.         rb2D = GetComponent<Rigidbody2D>();
    43.     }
    44.  
    45.     // Update is called once per frame
    46.     void Update()
    47.     {
    48.         switch (currState)
    49.         {
    50.             case (BossState.Idle):
    51.                 Idle();
    52.                 break;
    53.             case (BossState.Follow):
    54.                 Follow();
    55.                 break;
    56.             case (BossState.Die):
    57.                 break;
    58.             case (BossState.Attack):
    59.                 Attack();
    60.                 break;
    61.         }
    62.         if (!notInRoom)
    63.         {
    64.             if (IsPlayerInRange(range) && currState != BossState.Die)
    65.             {
    66.                 currState = BossState.Follow;
    67.             }
    68.             else if (!IsPlayerInRange(range) && currState != BossState.Die)
    69.             {
    70.                 currState = BossState.Idle;
    71.             }
    72.             if (Vector3.Distance(transform.position, player.transform.position) <= attackRange)
    73.             {
    74.                 currState = BossState.Attack;
    75.             }
    76.  
    77.         }
    78.         else
    79.         {
    80.             currState = BossState.Idle;
    81.         }
    82.     }
    83.  
    84.     private bool IsPlayerInRange(float range)
    85.     {
    86.         return Vector3.Distance(transform.position, player.transform.position) <= range;
    87.     }
    88.  
    89.     private IEnumerator ChooseDirection()
    90.     {
    91.         chooseDir = true;
    92.         yield return new WaitForSeconds(Random.Range(2f, 8f));
    93.         randomDir = new Vector3(0, 0, Random.Range(0, 360));
    94.         Quaternion nextRotation = Quaternion.Euler(randomDir);
    95.         transform.rotation = Quaternion.Lerp(transform.rotation, nextRotation, Random.Range(0.5f, 2.5f));
    96.         chooseDir = false;
    97.     }
    98.  
    99.     void Idle()
    100.     {
    101.         if (IsPlayerInRange(range))
    102.         {
    103.             currState = BossState.Follow;
    104.         }
    105.     }
    106.  
    107.     void Follow()
    108.     {
    109.         transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
    110.     }
    111.  
    112.     void Attack()
    113.     {
    114.  
    115.         if (!coolDownAttack)
    116.         {
    117.             switch (bossType)
    118.             {
    119.                 case (BossType.Melee):
    120.                     GameController.DamagePlayer(1);
    121.                     StartCoroutine(CoolDown());
    122.                     break;
    123.                 case (BossType.Ranged):
    124.                     GameObject bullet = Instantiate(bulletPrefab, bossFirePoint.position, bossFirePoint.rotation);
    125.                     bullet.GetComponent<Bullet>().GetPlayer(player.transform);
    126.                     bullet.GetComponent<Bullet>().isEnemyBullet = true;
    127.                     StartCoroutine(CoolDown());
    128.                     break;
    129.             }
    130.         }
    131.     }
    132.  
    133.     private IEnumerator CoolDown()
    134.     {
    135.         coolDownAttack = true;
    136.         yield return new WaitForSeconds(coolDown);
    137.         coolDownAttack = false;
    138.     }
    139.  
    140.     public void Death()
    141.     {
    142.         Destroy(gameObject);
    143.     }
    144. }
    145.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    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.
     
    Kurt-Dekker likes this.
  3. M3ntalll

    M3ntalll

    Joined:
    Jul 1, 2020
    Posts:
    8
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullet : MonoBehaviour
    6. {
    7.  
    8.     public GameObject hitEffect;
    9.     public bool isEnemyBullet = false;
    10.  
    11.     private Vector2 lastPos;
    12.     private Vector2 curPos;
    13.     private Vector2 playerPos;
    14.  
    15.     void OnCollisionEnter2D(Collision2D collision)
    16.     {
    17.         GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
    18.         Destroy(effect, 1f);
    19.         Destroy(gameObject);
    20.     }
    21.  
    22.     void OnTriggerEnter2D(Collider2D col)
    23.     {
    24.         if(col.tag == "Enemy")
    25.         {
    26.             col.gameObject.GetComponent<EnemyController>().Death();
    27.             Destroy(gameObject);
    28.         }
    29.  
    30.         if(col.tag == "Player" && isEnemyBullet)
    31.         {
    32.             GameController.DamagePlayer(1);
    33.             Destroy(gameObject);
    34.         }
    35.     }
    36.  
    37.  
    38.  
    39.     void Update()
    40.     {
    41.         if (isEnemyBullet)
    42.         {
    43.             curPos = transform.position;
    44.             transform.position = Vector2.MoveTowards(transform.position, playerPos, 5f * Time.deltaTime);
    45.             if (curPos == lastPos)
    46.             {
    47.                 Destroy(gameObject);
    48.             }
    49.             lastPos = curPos;
    50.         }
    51.     }
    52.  
    53.     public void GetPlayer(Transform player)
    54.     {
    55.         playerPos = player.position;
    56.     }
    57.  
    58. }
    59.