I have the player press E to stab with their sword. Which instantiates a trigger collider around the sword they cant see. If a monster collides with it they take damage and the whole bit. The problem is, the way I coded it spawns maybe 10 prefabs a second resulting in the monster taking a lot of damage really fast.
I messed around and coded to destroy the prefabs all but just as fast as they come in but it still bugs a good bit. What should i do to only instantiate one?

  • Code attached to sword only deals with knockback.

  • Animation code attached to player says “attacking” bool to true when player presses E, and instantiates the prefab.

    void FixedUpdate () {

     	if (Input.GetKey (KeyCode.E)) {
     		attacking = true;
     		attack();
     	}else attacking = false;
    
    
     	if (Input.GetKeyUp (KeyCode.E)){
     		attacking = false;
     	}
    

-Here is the instantiate code ** (also in the animation script)

void attack(){

	if (OneHandEquipped){
		instantiatedObj = (GameObject) PhotonNetwork.Instantiate(wepTriggerPrefabName, wepPoint.position, wepPoint.rotation, 0);}
	Destroy(instantiatedObj, time);
}

Instead of Input.GetKey use Input.GetKeyDown. Input.GetKey is called any time the button is pressed (which means you will call attack ~60 times if you hold the key for just one second), whereas GetKeyDown is called just once.