- Home /
Very Simple Melee Attack Collision Question
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 ?
Answer by Owen-Reynolds · May 22, 2011 at 02:38 AM
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;
...
didn't work, i tried using it but AI code just gone, in the panel it said look view is 0 or sumthing like that,simply when i tried doing such thing, none of them died.
But do you understand the idea of testing a lot to see exactly what is wrong? Did you check to see if your code snaps to -600 health right away, or stutters down? Do you get the idea of using a True/False to make sure something can only happen once? Those things will solve lots of problems.
Answer by Eli-Davis · May 22, 2011 at 12:33 AM
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.
There are the same number of Enters as Exits. You're probably thinking of OnCollisionsStay, which can run every frame.
Your answer
Follow this Question
Related Questions
OnTriggerStay2D only detecting trigger collisions while moving 3 Answers
Full Performance On Melee Combat 0 Answers
Recommended approach for melee combat system 2 Answers
melee combat 0 Answers