Objective #4: Destructible Box
In this article I want to cover a situation which pressing the interact button and holding the interact button do 2 different things to the same object.
This is a little bit different than the Zone Type interactions where 1 zone can be a press while the other is a hold type. This interactable zone is both. Let’s go!
At the end of this small game, you need to destroy a crate by interacting with it. When you press the interact button, one piece breaks off. If you hold it and let go, multiple pieces break.
The first situation is already set up pretty much. The Crate script has a BreakPart() that gets called when the onZoneInteractionComplete event is invoked.
This interactable zone is an Action type. So the InteractableZone script already has this logic built out from previous articles.
In my HoldInteraction() I don’t have logic built out for this type of zone though! Until now.
SO the onHold event is invoked when this method is called. I have the onHold event set up in the Crate’s script.
So when that event is invoked the InteractableZone_onHold() is called. I simply iterate through a loop. _numberToBreak is 2. SO when this loop happens it checks if i is less than 2. If so, destroy a piece. I increases by 1, so it goes through the loop again. Now I is 2. Now the loop is over.
Yay! Now when I press, I only destroy 1 piece, but when I hold the button 2 pieces break.