- Yeh so since implementing the player health class i get hurt walking into everything. I tried doing something similar to the projectile class where we reference the indestructible class and say !indestructble in the if statement but it has no effect.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
[SerializeField] private int maxHealth = 3;
[SerializeField] private float knockBackThrustAmount = 10f;
[SerializeField] private float damageRecoveryTime = 1f;
private int currentHealth;
private bool canTakeDamage = true;
private Knockback knockback;
private Flash flash;
private void Awake(){
flash = GetComponent<Flash>();
knockback = GetComponent<Knockback>();
}
private void Start (){
currentHealth = maxHealth;
}
private void OnCollisionStay2D(Collision2D other) {
EnemyAI enemy = other.gameObject.GetComponent<EnemyAI>();
if (enemy && canTakeDamage)
TakeDamage(1);
knockback.GetKnockBack(other.gameObject.transform, knockBackThrustAmount);
StartCoroutine(flash.FlashRoutine());
}
private void TakeDamage(int damageAmount){
canTakeDamage = false;
currentHealth -= damageAmount;
Debug.Log(currentHealth);
StartCoroutine(DamageRecoveryRoutine());
}
private IEnumerator DamageRecoveryRoutine(){
yield return new WaitForSeconds(damageRecoveryTime);
canTakeDamage = true;
}
}
Looking at the code above, you’re likely not taking DAMAGE from everything, but you’re definitely getting Knockback and flashing when you hit anything.
try putting all of this into a code block
if(enemy && canTakeDamage)
{
TakeDamage(1);
knockback.GetKnockBack(other.gameObject.transform, knockbackThrustAmount);
StartCoroutine(flash, FlashRoutine();
}
Thanks so much for your advice. I am not sure whats happened but the issue is resolved. I went further in the course and some of this code was refactored I think and now dosnt seem to cause the problem.