- 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;
}
}