Hello everyone, i have 1 Team A guy,1 Team B guy,they are enemies to each other and they all have AI scripts, but i have problem about the collision of the swords. Team A guy have 300 health,which means he will be dead in 6 hits,but he’s dying too quickly,because the function always happens when sword it’s colliding,i want to make it to happen one per colliding,here is my code:
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag=="ZulDurak") {
ZulDurakAI1.healthzul -= 50;
}
}
i tried OnTrigger but didn’t work,any ideas ?
instead of onCollisionEnter, try OnCollisionExit, so the script is only called once the collider comes off the player, or atleast I think That will work.
The sword is either a Collider or a TriggerBox (a Collider with isTrigger checked.) You can only use the Trigger functions for trigger boxes, and vice-versa. I’d guess you want the sword to be a triggerBox, so it can swing through without doing some wierd pushing stuff.
Your code should work. My guess is that it hits an arm, then the body, maybe bounces off and hits again, then clips the same arm, then the other arm… . If health always drops to 0 immediately, my guess is wrong.
One fix is to add a bool so it can only do damage once/swing:
// I'm used to C#, so guessing javascript:
var swordReady : bool = false;
// have to "look up" swordReady if different scripts
if( [starting swing] ) swordReady=true;
// in OnTriggerEnter:
if (swordReady && collision.gameObject.tag=="ZulDurak") {
swordReady=false;
...