So I’m making a horror game and one of the features is vending machine that over time gets broken and if you don’t repair it you get jump scared. Tho the problem is that I don’t know how to make those phases of being perfectly fine to broken to jumpscare. I tried making a timer but for every phase there should be a timer and it got really messy and I got confused.
The code:
When you you get the jump scare? When trying to use the vending machine?
It seems like you have a few things going on:
The state of the machine – “Fine,” “Broken,” and “BrokenWithJumpScare”
The user attempting to repair the machine – if the state is “BrokenWithJumpScare,” do the jump scare, else if the state is Broken, set it to Fine, else do nothing.
The user attempting to use the machine – if the state is BrokenWithJumpScare, do the jump scare, else if the state is Fine, give the user the item, else do nothing.
Displaying the machine state. Specifically, when state goes from “Fine” to “Broken,” swap out a broken mesh (or material?) and when state goes from “Broken” back to “Fine,” swap out the mesh (or material) again.
You really only have one variable, which is “time since last repaired.” Everything else can be derived from that.
So, add a function “DetermineState” which returns the state, by looking at “time now” minus “time it was last repaired.”
In each of the event triggers (repair, interact, and tick,) call this function to determine the current state, and take the appropriate action.
Checking this every tick, just to figure out whether to swap to broken or not, while totally fine for a single machine, could be slighlty optimized by setting a timer when the machine starts out (or gets repaired.) Totally not necessary for this use case, though.