Hi,
I’m making a physics game where if you hit something with great enough velocity you take damage.
What I’m trying to do here is to add invincible time after on collision. I have a coroutine with a float time that makes invincible true after on collision. I thought this would work and been searching everywhere but can’t find answers.
Thanks for any help.
public int attackDamageLow = 10;
public int attackDamageMid = 20;
public int attackDamageHigh = 30;
public float hitDelay =5f;
private bool Invincible = false;
GameObject player;
Health playerHealth;
//EnemyHealth enemyHealth;
bool playerInRange;
float timer;
// Use this for initialization
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("MainPlayer");
playerHealth = player.GetComponent<Health> ();
//enemyHealth = GetComponent<EnemyHealth> ();
}
void OnCollisionEnter (Collision other)
{
if (!Invincible) {
if (other.gameObject == player) {
float mag = other.relativeVelocity.magnitude;
if (mag > 40f) {
AttackHigh ();
Debug.Log ("S");
StartCoroutine (Delay ());
} else if (mag > 20f) {
AttackMid ();
Debug.Log ("M");
StartCoroutine (Delay ());
}
else if (mag > 10f) {
AttackLow ();
Debug.Log ("H");
StartCoroutine (Delay ());
}
}
}
}
void AttackLow ()
{
//timer = 0f;
if(playerHealth.currenthealth >0)
{
playerHealth.takeDamage (attackDamageLow);
}
}
void AttackMid ()
{
//timer = 0f;
if(playerHealth.currenthealth >0)
{
playerHealth.takeDamage (attackDamageMid);
}
}
void AttackHigh ()
{
//timer = 0f;
if(playerHealth.currenthealth >0)
{
playerHealth.takeDamage (attackDamageHigh);
}
}
IEnumerator Delay()
{
Invincible = true;
Debug.Log ("True");
yield return new WaitForSeconds (hitDelay);
Debug.Log ("false");
Invincible = false;
}
}