So I'm trying to make an enemy but when there are 2 enemies when the first one dies the second one just go in minus in health and I just can't seem to fix it can anyone help me?
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Collections;
public class StoneGolemController : MonoBehaviour
{
#region Variable Declarations
GameObject Player;
public List Targets;
public GameObject Orb;
public TextMeshProUGUI displayText;
public float MoveSpeed = 2f;
float searchRadius = 100f;
float meleeRadius = 2f;
float throwRadius = 5f;
public float maxThrowingStones = 30f;
public float currentThrowingStones;
public Transform ThrowingStone;
float currentTargetDistance;
public static StoneGolemController instance;
public float MaxHealth = 500;
public float CurrentHealth;
public static int DeathHealth = 0;
public GameObject StoneGolem;
public bool onCooldown = false;
public float delay = 3f;
GameObject myTarget;
#endregion
void Awake()
{
Player = GameObject.FindGameObjectWithTag("Player");
Orb = GameObject.FindGameObjectWithTag("Orb");
StoneGolem = GameObject.FindGameObjectWithTag("StoneGolem");
instance = this;
CurrentHealth = MaxHealth;
Targets = new List(GameObject.FindGameObjectsWithTag("Building"))
{
Orb
};
//ThrowingStone.transform.position = StoneGolem.transform.position;
currentThrowingStones = maxThrowingStones;
}
void Update()
{
{
TargetFinder();
if (myTarget == null)
currentTargetDistance = searchRadius;
else
Move(myTarget.transform);
{
if (currentThrowingStones > 0)
{
if (currentTargetDistance <= throwRadius && !onCooldown)
{
StartCoroutine(ThrowDelayLogic());
MoveSpeed = 0f;
}
}
if (currentThrowingStones > 0)
{
if (currentTargetDistance >= throwRadius && !onCooldown)
{
MoveSpeed = 2f;
}
}
if (currentThrowingStones <= 0)
{
MoveSpeed = 2f;
if (currentTargetDistance <= meleeRadius)
{
DoMelee();
}
}
}
if (CurrentHealth <= DeathHealth)
{
print("Enemy Has died!!!");
Destroy(StoneGolem);
}
}
void TargetFinder()
{
foreach (var x in Targets)
{
float distance = (transform.position - x.transform.position).magnitude;
if (distance < currentTargetDistance)
{
myTarget = x;
//displayText.text = "My target: " + x + "\n Distance to Target: " + distance; //skal fjernes nå det er helt klart
currentTargetDistance = distance;
}
}
}
void Move(Transform t)
{
transform.LookAt(t);
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
}
void DoMelee()
{
//if (hit.collider.tag == "Player")
{
//PlayerController eHealth = hit.collider.GetComponent();
//PlayerController.instance.CurrentHealth -= Random.Range(minWeaponDamage, maxWeaponDamage + 1);
//print("Player took some damage");
}
myTarget = null;
currentTargetDistance = searchRadius;
}
void DoThrow()
{
myTarget = null;
currentTargetDistance = searchRadius;
Instantiate(ThrowingStone);
currentThrowingStones--;
}
IEnumerator ThrowDelayLogic()
{
DoThrow();
onCooldown = true;
yield return new WaitForSeconds(delay);
onCooldown = false;
}
}
}
↧