Attacking an enemy (2D platformer)
What i have currently works OK but its far from where i want it. So when the player presses E they stab with their sword creating a trigger, damaging mobs if they touch it, knocking them back and playing an animation. So far it works but the player can spam E and spawn maybe 3 or 4 triggers doing more damage than they should. I want a combo system where at the end of the stab they can press E again in a small time window and it combos with another swing upwards and same thing again downwards.
Here is my code.
Sword script is for knockback of the enemy only.
Monster script just deals with the animation and adjusting health. redflash is an attached animation and hurt is an animation in the animator.
void OnTriggerStay2D(Collider2D other) {
if (other.CompareTag ("PhysicalWep")) { animation.Play("Ghost_RedFlash"); currentHealth = currentHealth - 1; Debug.Log(currentHealth); hurt = true; } }
Ok here is the bread and butter, this is attached to the player.
void Update(){ deeztriggers = GameObject.FindGameObjectsWithTag("PhysicalWep");
if (grounded){ if (Input.GetKeyDown (KeyCode.E)) { StartCoroutine(attack()); }} } IEnumerator attack(){ if (OneHandEquipped){ attacking = true; instantiatedObj = (GameObject) PhotonNetwork.Instantiate(wepTriggerPrefabName, wepPoint.position, wepPoint.rotation, 0);} yield return new WaitForSeconds (0.25f); Destroy(instantiatedObj, time); foreach (GameObject PhysicalWep in deeztriggers){ Destroy(PhysicalWep);} attack2ready = true; Debug.Log ("attack2 ready"); if (attack2ready){ if(Input.GetKeyDown (KeyCode.E)){ attackingX2 = true; }else attackingX2 = false;} yield return new WaitForSeconds (1f); attack2ready = false; attacking = false; }