- Home /
Specify which trigger does what
EDIT: Suitably humbled, I'm adding the code below. EDIT2: Strange, even with no weapon, and therefore no way to take damage, the npc still loses hp, playing the audio sound, and eventually dying. I'm confused. Here it is: I have a troll npc who walks toward the player, and holds a club, used for attacking. There is a box collider with a trigger attached and a script (on the club, which is attached to the troll's armature) which causes damage to the player on trigger enter. Everything's cool there. Separately, I have attached a health script for the troll on the parent object, with a different box collider and trigger attached. Now, in my noob brain I'd imagine that if the club hit me (looking for player tag in ontriggerenter) I'd take damage, which I do, but so does the npc, even though the two box colliders do not overlap. And so I'm wondering if I should go about things a different way, or maybe simply add an "attack" variable to the attack component which temporarily stops the npc from taking damage? Thanks for any and all feedback. God bless.
using UnityEngine;
using System.Collections;
public class GolgorLife : MonoBehaviour{
float enemyHealth;
bool enemyDeadBool;
void Start() {
{
enemyHealth = 199;
enemyDeadBool = false;
}
}
void OnTriggerEnter(Collider triggerObject) {
{
//assuming the sword object is named sword
if (triggerObject.gameObject.name == "sword")
{
enemyHealth += - 25;
audio.Play();
}
else
{
if (triggerObject.gameObject.name == "club")
{
enemyHealth += - 10;
audio.Play();
}
}
}
}
void Update()
{
if (enemyHealth <= 0)
{
//may be useful to not just instantly destroy
//you can add +experience or death animation etc...
//animation.Play("die");
enemyDeadBool = true;
}
if (enemyDeadBool == true)
{
//do anything to reward player or death animations etc...
Destroy(gameObject,5);
}
}
}
Who's got the club/sword - you or the enemy? And who's got this script attached to them? It sounds like the enemy is detected a collision with their own weapon, which would cause the health drop/audio you describe.
it looks like your colliders might be overlapping anyway. I can't really grasp what you are trying to do, although i don't know c# so that could easily be why it looks so messed up to me.
if you haven't already, try putting the objects on different layers and set their layer collision matrix to not collide with each other.
(you can find the layer collision matrix under project settings physics)
good luck!
Thankstanoshimi, both the player and npc have a club, that makes sense! Will check when I get home. Thanks!
EDIT: It worked!!!!! I love when the simple answer is the best one. :)
If you convert that to an answer I'll be happy to accept it :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613124033im_/https://answers.unity.com/themes/thub/images/avi.jpg)