Hello! I am using a player character that has a child with a trigger object which checks if enemies are in the array and able to take damage, or outside of it and there is no enemy to apply damage to. The control for this trigger is the right analog stick for the player. So it changes direction according to the axis input. That code looks like this:
void Update() {
[...]
float rightHorizontal = Input.GetAxis ("P2_RightStickHorizontal");
float rightVertical = Input.GetAxis ("P2_RightStickVertical");
if (rightHorizontal != 0f || rightVertical != 0f) {
float angle = Mathf.Atan2 (rightVertical, rightHorizontal) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (new Vector3 (0, 0, angle));
}
This all works fine and dandy, but there is one rather big issue concerning applying damage:
If I turn my character around instantly with the right stick, that means the attack and trigger should happen at the same frame, but the attack is not triggered. For me to be able to damage the enemy, the trigger collider must already be over an enemy before the damage function gets called. The check for an enemy is an “OnTriggerEnter2D” and an “OnTriggerExit2D” for adding or removing enemy from list.
This is my damage function:
void Damage () {
float addedDamageRange = Random.Range (2.5f, 5.5f);
float damageDealt = attackDamage + addedDamageRange + strength * 0.25f;
float rightHorizontal = Input.GetAxis ("P2_RightStickHorizontal");
float rightVertical = Input.GetAxis ("P2_RightStickVertical");
GameObject _attackObject = GameObject.Find ("GeorgeAttack");
ShockwaveAttackScript attackAnim = (ShockwaveAttackScript)_attackObject.GetComponent ("ShockwaveAttackScript");
if ((rightHorizontal != 0f || rightVertical != 0f) && meleeAttackCD == 0f) {
attackAnim.isAttacking = true;
foreach (Collider2D enemy in colliderList) {
EnemyHealth eh = (EnemyHealth)enemy.GetComponent ("EnemyHealth");
eh.hpLost = damageDealt;
eh.curhp -= damageDealt;
if (!enemy || eh.curhp <= 0) {
colliderList.Remove (enemy.collider2D);
return;
}
}
meleeAttackCD = 1f - agility * 0.008f;
audio.PlayOneShot (meleeAttack);
}else {
attackAnim.isAttacking = false;
}
}
So, what I want to do here is to maybe add a minimal time to wait before the damage applies, or get the trigger collider to work just a frame faster(?). Considering the damage function is just called in the main update like this (below), I am confused on how to make things work:
void Update () {
[...]
//MeleeAttack Method called
Damage ();
And yes, this is all based on the same script, initially. Any ideas, hints or tips to what I could do would be greatly appreciated. It’s like, if it only waited one frame it would probably apply the damage!