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