Hello guys !
I am making a game, and I have a player you can translate on enemies to kill them but since the foes are instantiate with a prefab and a spawnpoint I can't hit them. Maybe because the health isn't in an array. But I can't manage to do it. The foe Health is from there script.
GameObject GestureAttack()
{
//Get all the foes
GameObject[] Foes = GameObject.FindGameObjectsWithTag("Foe");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
//Find the nearest to attack him
foreach (GameObject go in Foes)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
if (recupgeste == "ga_circle_01" && recupgestescore > 0.60 || Input.GetKeyDown(KeyCode.Space)) {
transform.position = Vector3.MoveTowards(transform.position, closest.transform.position, 100);
}
return closest;
//THE PROBLEM
//For each foe I have I hit them once (FoeHealth is the script (In start: FoeHealth = Foe.GetComponent();)and currentHealth the life variable in the script).
foreach (GameObject go in Foes)
{
if (FoeHealth.currentHealth > 0 /*attackphas*/)
{
Debug.Log("TOOOK");
// ... damage the player.
FoeHealth.TakeDamage(attackDamage);
}
}
}
Thanks in advance ^^
↧