This interaction system provides a simple and modular solution for handling interactions between an interactor (InteractorComponent) and interactable actors (InteractableComponent) in Unreal Engine. The system facilitates communication between these components through a defined interface, allowing you to easily implement and customize interactions within your projects! Events are also exposed, so you can have unique per object outcomes if you like. It works by raycasting to the hitbox of the actor, and generally works best in a 1st person setup.
The Interactor component is responsible for initiating interactions with interactable objects. It is typically attached to the player character, or any other actor that needs interaction capabilities.
- Attach to Character: Add the Interactor component to your player character or desired actor.
- Input Action: Configure an input action for interaction (e.g., "Interact"). Bind this action in your player controller or wherever input handling is managed.
The Interactable component represents objects in the world that can be interacted with. It responds to interaction requests from Interactors.
- Attach to Actors: Add the Interactable component to any actor you want to make interactable.
- Implement Interface: Make sure the actor implements the IInteractableInterface interface. This interface defines the methods needed for interaction.
- Interaction Interface (IInteractableInterface)
- The interface defines the communication contract between Interactors and Interactables. It includes methods like OnInteract that need to be implemented by Interactable actors.
Attach the Interactor component to your player character or actor that will perform interactions. Attach the Interactable component to any actor you want to make interactable.
As we are using the enhanced input system, define an input action for interaction (e.g., "Interact"). Assign the InputAction in the component via the blueprint inspector, or you can use constructor helpers in the code.
Create UUserWidgets (UI) for any interactive feedback you want to provide to the player during interactions. These widgets are triggered by the Interactable actors during interaction events. You'll need to make an "E To Interact" and "Cannot Interact" widget. Assign these in the blueprint too.
As we are using events, you can add your own logic in blueprint (or code!) as required.
