How would I code a enemy that only hurts occasionally

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JAA2000

Hello, I’m making a game, in which an enemy is a crocodile that is safe to walk on when its mouth is closed, but when its mouth is open, it will hurt the player. How exactly would I code this? Thank you.

:bust_in_silhouette: Reply From: AlbGD

To give an exact answer, we would need to know how your player is detecting the damage and the overall setup of your nodes. Otherwise it won’t adapt very well.

Having said that, one idea is to set up a bool variable canTakeDamage (or similar). And link the animation of the crocodile to that variable. You may need to use custom signals (again we don’t know your setup).

The code would look something like this:

var canTakeDamage : bool = false

func healthDown():
   if canTakeDamage:
      //your code to react to damage

Hope this helps giving you some ideas to apply to your game.