- Home /
How can I attach a colldier that moves with the enemies sword in an attack animation?
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<Animation>().Play("die");
sAlive = false;
Destroy(gameObject.transform.parent.gameObject.GetComponent<CapsuleCollider> ());
} 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<Animation> ().Play ("attack");
GameObject.FindGameObjectWithTag ("sword").GetComponent<Weapons> ().healthPt -= 3;
}
}
If the sword is an actual GameObject (a child of the character holding it), then just attach the collider to the sword, set it to IsTrigger = true, and add a simple script that registers collisions; Take a look at the OnTriggerEnter method.
Your answer
Follow this Question
Related Questions
Colliding Melee Weapons 2 Answers
hold key unequip sword 0 Answers
How could I combine melee weapons and rays? 1 Answer
A Fighting Games Hit boxes 1 Answer
Melee attack comblat 1 Answer