This is the current code I have on all of my enemies, it checks to see if the character is within range, and then it plays the attack animation and removes health from the player. I want to have it set up so a coliider is on the sword of the enemy, that way it only reduces damage when you are actually hit by the sword, not just being close enough How would I implement this?
void Update () {
if (spartanPt < 0 && sAlive == true) {
gameObject.transform.parent.gameObject.GetComponent().Play("die");
sAlive = false;
Destroy(gameObject.transform.parent.gameObject.GetComponent ());
} else if (sAlive == true) {
Vector3 heading = GameObject.FindGameObjectWithTag ("mazeDude").transform.position - transform.position;
float distance = heading.magnitude;
Vector3 direction = heading / distance;
if (heading.sqrMagnitude < maxRange * maxRange) {
gameObject.transform.parent.gameObject.GetComponent ().Play ("attack");
GameObject.FindGameObjectWithTag ("sword").GetComponent ().healthPt -= 3;
}
}
↧