In the game I’m working on, I am noticing something strange that I do not want to happen. Whenever I have the player attack what is infront of them, it only works once. If the enemy stands still, and the player stands still while attacking, the player can not harm the enemy after the first swing. Either the enemy or the player has to move again in order for the attack to actually inflict damage upon said enemy. What I want to know is how to fix this, so I can have both the player and enemy stand still and still be able to inflict damage upon one-another.
What I have for an “attack” is simple. The player approaches the enemy, clicks, and that stops the player’s movement while the attack animation plays, which enables an object that is a child object of the Player, with this object specifically being a slash effect. This object has a trigger attached to it, which activates on any enemy it touches, supposedly. The slash also has a script attached to it which does the actual harming, said script being called “HurtEnemy”, which can be seen here:
HurtEnemy Script
using UnityEngine;
using System.Collections;
public class HurtEnemy : MonoBehaviour {
private int damageToDeal;
public Transform hitPoint;
public GameObject damageNumber;
private bool justCrit;
private int baseDamage;
public int critChance;
private PlayerStats pATK;
void Start () {
pATK = FindObjectOfType<PlayerStats> ();
damageToDeal = 0;
baseDamage = 0;
}
void Update () {
}
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Enemy") {
baseDamage = pATK.currentATK;
damageToDeal = baseDamage;
damageToDeal += Random.Range (-1, 2); //Adds a bit of randomness to damage dealt.
if (Random.Range (1, 100) <= critChance) { //Possibility of crit, exact chance able to be changed via Unity interface.
damageToDeal *= 2;
justCrit = true;
}
other.gameObject.GetComponent<EnemyHealthManager> ().HurtEnemy (damageToDeal);
var clone = (GameObject)Instantiate (damageNumber, hitPoint.position, Quaternion.Euler (Vector3.zero));
if (justCrit) {
clone.GetComponent<FloatingNumbers> ().damageNumber = damageToDeal;
clone.GetComponent<FloatingNumbers> ().displayNumber.color = Color.yellow;
justCrit = false;
} else {
clone.GetComponent<FloatingNumbers> ().damageNumber = damageToDeal;
clone.GetComponent<FloatingNumbers> ().displayNumber.color = Color.white;
}
damageToDeal = baseDamage;
} else if (other.gameObject.tag == "Destructable") {
other.gameObject.GetComponent<DestructableObjectManager> ().KillObject ();
}
}
}
The actual damage is dealt when this script calls another script on the object it touches, called “EnemyHealthManager”, specifically a “HurtEnemy” function. The script in question can be seen here:
EnemyHealthManager Script
using UnityEngine;
using System.Collections;
public class EnemyHealthManager : MonoBehaviour {
public int enemyMaxHealth;
public int enemyCurrentHealth;
private PlayerStats thePlayer;
public int expUponDeath;
void Start () {
enemyCurrentHealth = enemyMaxHealth;
thePlayer = FindObjectOfType<PlayerStats> ();
}
void Update () {
if (enemyCurrentHealth <= 0) {
thePlayer.AddExperience (expUponDeath);
Destroy (gameObject);
}
}
public void HurtEnemy(int damage) {
enemyCurrentHealth -= damage;
StartCoroutine ("HurtColor");
}
public void MaxHealth() {
enemyCurrentHealth = enemyMaxHealth;
}
IEnumerator HurtColor() {
for (int i = 0; i < 3; i++) {
GetComponent<SpriteRenderer>().color = new Color (0.8f, 0.6f, 0.8f);
yield return new WaitForSeconds (.1f);
GetComponent<SpriteRenderer>().color = Color.white;
yield return new WaitForSeconds (.1f);
}
}
}
This health manager deals with the enemy’s, well, health. Once the enemy’s HP reaches zero, the gameobject of said enemy is completely destroyed, and the player given a set amount of experience.
With all that out of the way, it sounds like everything should be perfectly fine, right? I’d think so. Except for the one issue that is still bugging me non-stop, the OnTriggerEnter2D on the slash object (the one with the “HurtEnemy” script) does not activate a second time, even once the slash object’s active variable is turned on and then off. How can I make it so everytime the player attacks, the OnTriggerEnter2D works again upon the slash being re-enabled?