Hello!
I have one Problem that my Object is attacking only one enemy and then the other one get not harmed. (because of my Awake Function) but I dont know how to fix this issue into the Update Function (The FindwithTag & Healththing(?))
Here is my code so far:
using UnityEngine;
using System.Collections;
public class SkeletonWarriorAttack : MonoBehaviour
{
public float timeBetweenAttacks = 0.5f; // The time in seconds between each attack.
public int attackDamage = 13; // The amount of health taken away per attack.
GameObject playereast; // Reference to the player GameObject.
ElvenWarriorHealth eastHealth; // Reference to the player's health.
SkeletonWarriorHealth westHealth; // Reference to this enemy's health.
bool playerInRange; // Whether player is within the trigger collider and can be attacked.
float timer; // Timer for counting up to the next attack.
void Awake ()
{
// Setting up the references.
playereast = GameObject.FindGameObjectWithTag ("PlayerEast");
eastHealth = playereast.GetComponent ();
westHealth = GetComponent();
}
void OnTriggerEnter (Collider other)
{
// If the entering collider is the player...
if(other.gameObject == playereast)
{
// ... the player is in range.
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
// If the exiting collider is the player...
if(other.gameObject == playereast)
{
// ... the player is no longer in range.
playerInRange = false;
}
}
void Update ()
{
// Add the time since Update was last called to the timer.
timer += Time.deltaTime;
// If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
if(timer >= timeBetweenAttacks && playerInRange && westHealth.currentHealth > 0)
{
Attack ();
}
}
void Attack ()
{
// Reset the timer.
timer = 0f;
// If the player has health to lose...
if(eastHealth.currentHealth > 0)
{
// ... damage the player.
eastHealth.TakeDamage (attackDamage);
}
}
}
Maybe it is possible to do it with something like:
GameObject[] targets = GameObject.FindGameObjectsWithTag ("PlayerEast");
foreach (GameObject target in targets)
{
//tell the update function that this target is now "Targeted" and is going to be attacked! (?)
}
but I don't know how to get any further with this issue...
Thanks for your time!
Flowered
↧