So I’ve programmed plenty of enemies with 1 hit point- bounce on them once, they die.
I’ve been trying to program in an enemy with 2 hit points, or to die after the second time you jump on them. Here’s the code here.
I already have a pretty good idea of whats wrong. You jump on the enemy, and they die right away, without having to jump on them again. I’m thinking perhaps the code goes so quickly, that the player is still in collision with Trash after the scene variable changes, but even though I know that’s the issue, I’m stumped on the best way to program a fix.
I’ve tried doing timers for example, for the scene variable to change after a certain amount of time, but I screwed that up too and it didn’t work. I’ve been a bit too stumped on this issue, so I thought it would be better you ask you guys! An outside perspective is probably all I need, I think I’ve looked at it too long and it’s stumped me.
Thanks a ton!
You are right. The code goes quickly. To fix that, try using Trigger Once.
The problem is that the first event increments the value of TrashHit, and this satisfies the conditions of the second event. So the second event gets run straight after the first event has executed.
I think what you need are 2 object variables on the Trash object; one that keeps count of the number of times it’s been hit (a number variable, call it PlayerCollisionCount), and another that tracks whether the Trash object is in collision with the Player (a boolean variable, call it IsCollidingWithPlayer). The second variable is reset when the Player and Trash are not in collision.
When the player is in collision with a Trash object, check Trash.IsCollidingWithPlayer is false, and increment PlayerCollisionCount if so. If PlayerCollisionCount > 1, delete the Trash object.
as he said, I happen upon this a lot, I usually fix it by running a short-timer to have a cooldown of say 0.01, enough that the tick won’t run the second command in the same tick, just make sure both the first and second events won’t run without the timer and reset it at the beginning. That should make the code only run once.
Yes!! YES!! I did a quick timer, combined with 2 object variables, and I got it! It also fixed the secondary issue of any other ‘trash’ enemies on the screen changing their animation to the ‘damaged’ animation.
This makes so much sense! Thank you guys, I don’t know how I’d program a game without this forum helping me!
Here’s how it turned out:
(Her getting wounded after jumping on the other trash needs to be fixed but I think I know how to do that already.)
Thanks so much!
np. Glad it works! Have a nice day!