Melee system causing unwanted Multiple hits.

So while utilizing the fps tutorial as a base for my game, i began implementing a simple Melee system (weapon that causes melee damage).

My approach is pretty simple. Using a trigger volume (cube trigger) in front of the player a script attached to it checks two conditions and fires off a damage result to the enemy character. The conditions are, " Is object triggering volume taged ‘enemy’?" and the second being, “Is player pressing the Fire2 key?” when both conditions are met the script calls the “applydamage” function on the triggering object (enemy) via a ‘target’.SendMessage(“ApplyDamage”, 50 ) .

this system seems to render damage just fine, but the issue is it almost always fires more then once. I’ve tried using WaitForSeconds() to add a cool down but it has no effect, at this point i’ve set up a condition that states once the object fires once, disable a key variable in the root if statement which should stop a second fire, and surprisingly it still multi-fires.

This issue has grown to be very annoying and I feel I currently lack the programming experience to solve the issue. Below is the script used to render damage, I’d greatly appreciate any input on the root of the issue and how to solve it.

function OnTriggerStay (Thing : Collider) 
{
	var Check = 1;
		
		if (Check == 1)// should only allow the damage to be applied once.
		{
				if (Thing.tag == "enemy"  Input.GetButtonDown ("Fire2"))
				{
				
					Thing.SendMessage("ApplyDamage", 50, SendMessageOptions.DontRequireReceiver);
					
					Check = 0; //should turns off damage
					Debug.Log(" Check Variable should equal 0, disabling this code block from triggering more then once:" + Check.ToString());

						
				}
			
		}	
			
		
	
}

this is because the Check variable is defined inside the scope of the OnTrigger function, this resets Check to 1 everytime the function gets called. you need to define Check outside of the function in order to keep it’s value between 2 function calls

Great catch, totally missed it, thank you! appears to be working.

You could also use GetButtonUp instead of GetButtonDown, that way it will only be triggered once and you wont need the control variable at all.