In this game, the player is basically tasked with interacting with objects in the scene. When these objects are interacted with, the game proceeds. This article will cover how to make the E button and the West button on game pads handle different KINDS of interactions whether they are simple presses or hold interactions. Lets go!
The first objective that appears to the player is a little green zone. When the player enters the green zone a prompt appears that says “Press the E Key to Pickup C4.”
C4!?!? We get to blow stuff up soon?! Oh yes. Let’s proceed.
Currently, the legacy system is implemented for this.
I want to remove the need for an update method in this instance because there are SEVERAL interactable zones that all share this same script. Its a small game, so it doesn’t REALLY matter, but practice IS perfect, so that’s what I’m going to do.
First, I need to register my performed action in Start(). I’ll register this in the Player class since this is part of the Player Action Map.
As you can see, I have created an event to handle interaction.
Good ol’ delegates and events to the rescue. So now all I need to do is take the code from my update method in the InteractableZone class and have it called when this event is raised in that class.
This has completely removed the need for an Update method. Currently I’m only handling press events though. The first scenario in this game consists of three simple press actions. Pick up C4, place the C4, and then detonate!
The next interaction type will be a hold interaction to hack the laptop to the right of the player when the car detonates. The way I have this set up is ok, but I want to separate some things out.
I’ve actually renamed my initial event in my Player class to PressInteract and I created another called HoldInteract.
And my Interacted() this event calls looks a little different too.
Now I’m checking whether the interaction type is a hold interaction or a press. My Input Action for this particular situation looks like this.
If I hold the interact button for 1.5 seconds, the performed action will register as a hold interaction. Otherwise, it will register as a press.
I’ve created a new method to handle hold interactions here in the InteractableZone class.
So if _inZone and the zone type is
HoldAction, perform the Hold Action.
As we can see here, this particular interaction zone is a HoldAction type.
So when I hold the interadct button for 1.5 seconds, the PerformHoldAction method gets called. This causes a fancy loading/hacking UI to pop up. When its done, the next zone becomes interactable and controls are then passed off to the camera system.
I will dig a little deeper into this concept in the following articles. For now, we have not only converted all our interaction inputs to the new input system, but we have completely stripped the Update() out of the InteractableZone class. I think thats a win win. :)