So, I am working on a remake of Zelda for NES and was running into a problem concerning the heart system. I have 3 hearts and each of them has 2 halves. I want it so when my player (Link) comes into contact with an enemy, he loses one half of a heart.
I have it so when Link comes into contact with an enemy, it destroys one half of a heart, but I don’t know how to make it so it doesn’t destroy every single heart when I collide with an enemy. Is there a way to call multiple trigger events?
I am working in C#, so that syntax is much appreciated!
Thank you,
Joe
2 Answers
2
Im guessing you remove hearts whenever there is a collision? The problem with that is when you touch an enemy, Link is colliding every update as long as theyre touching so he will take damage like 10 times a second until they move apart.
So you will need a brief ‘invincibility’ period where he cant take damage after he takes damage. I think in game it was represented by him flashing?
Adding a timer functionality to it would be the way to go, as well as adding a visual cue when you cant take damage
Use something like this:
void OnCollisionEnter(Collision other) {
if (other.gameObject.tag == "TempEnemy" && invincibleTime <= 0f ) {
health -= 0.5;
invicibleTime = 3f; // 3 seconds of invincibility
}
}
xy.xylophone is not wrong, but he is not exactly right either. “The problem with that is when you touch an enemy, Link is colliding every update as long as theyre touching”, this only applies when you use OnCollisionStay(), it called every frame. In other hand, OnCollisionEnter() is called once when the collision occurs.
An example for void OnCollisionStay( Collision other ) would be something like this:
void OnCollisionStay( Collision other ) {
if( other.gameObject.tag == "BloodSuckingBat" ) {
health -= 1 * Time.deltaTime;
}
else if( other.gameObject.tag == "PoisonSwamp" ) {
health -= 2 * Time.deltaTime;
}
}
I feel this issue may come up a fair amount to new developers, next time when making a question you could make it more broad like: Enemy damages character several times per impact rather than once. That was a pretty avg title but it can apply to more situations and easier to find with a search too :D
– xt-xylophone