Hi, I’m working on a script where the player takes 1 damage when being crushed in between two objects. However, the issue I’m having is that if the objects are traveling fast enough, the player takes 2 damage.
What I’m currently trying to do is check if a function has recently been called, and if so, to not call the function again. I also tried adding an invulnerability function, but I wasn’t able to get that to work. Here’s the code as it is-- still learning coding in general, so I apologize if there’s some bizarre practice going on. Thank you!
PlayerController script:
if (isCrushed)
{
RaycastHit2D hit = new RaycastHit2D();
hit = Physics2D.Raycast(transform.position, Vector2.up, 5.0f, whatIsHexPlate);
if (hit.collider != null && hit.collider.tag == "HexPlate")
{
hit.collider.gameObject.transform.GetComponent<Rigidbody2D>().AddForce(upHpm);
}
//Deduct 1 health.
playerHealth.TakeDamage(1);
}
PlayerHealth script:
public void TakeDamage(int amount)
{
damaged = true;
currentHealth -= amount;
if (currentHealth <= 0 && !isDead)
{
Death();
}
}
public void Invulnerable(float time)
{
damaged = false;
invulnerableTimeMax = time;
if (Time.deltaTime < invulnerableTimeMax)
{
damaged = false;
isInvulnerable = true;
}
else if (Time.deltaTime >= invulnerableTimeMax)
{
isInvulnerable = false;
}
}