The player needs to be able to interact with the game world, that’s what the interaction controller is used for! Picking up items, talking to an NPC … every Interact type event needs to be recognized by the player when it’s within range.
The interaction controller is a game object with a Rigidbody, a Collider (used as trigger) and the Interaction Controller component attached – first we need to create this object and turn it into a prefab. Then we’ll use this prefab in ORK Framework and have it automatically added to the player in the game. This way it doesn’t matter who the player is, he will always have the interaction controller ready to go, even if you change the player in the running game.
Creating the interaction controller prefab
Let’s make this really easy by using the ORK Scene Wizard – open it Unity® menu: Window > ORK Scene Wizard.
Now use the scene wizard to create the interaction controller object: Create Object > Interaction Controller.
The scene wizard will create a new game object and add all necessary components (Collider, Rigidbody, Interaction Controller). A BoxCollider has been added by default – if you want a different collider, simply remove the BoxCollider component and add a different collider (don’t forget to enable the Is Trigger setting).
Now create a prefab out of this object by dragging it (it should be named Interaction Controller) from the scene hierarchy on the project tab. We’re done with the prefab, remove it from the scene.
Adding the interaction controller
Open the ORK Framework editor using the Unity® menu: Window > ORK Framework.
Navigate to Base/Control > Game Controls and edit the following settings.
The interaction settings handle everything related to interacting with the game world, i.e. wherever the Interact start type is used for interaction components (like Event Interaction, Item Collector, etc.).
- Interact Key
This defines which input key is used to start interactions when they are within the interaction controller of the player.
- Max Click Distance
Set to 3.
This defines how much distance between the player and the interaction is allowed to start the interaction by clicking/touching on it.
If you don’t want to interact by click/touch, set this to 0.
If click/touch should be allowed without a distance limitation, set this to -1.
- Add Automatically
Enable this setting.
The interaction controller will automatically be added to any player game object.
- IC Prefab
Select the prefab we just created (Interaction Controller).
Set to X=0, Y=1 and Z=1.
This is the offset the prefab will have to the player.
Depending on your player’s game object, you need to vary the Offset setting. To find the best offset, simply pause the game when testing and place the Interaction Controller object where it suits your player best and use it’s position (found in the inspector of the transform) as offset.
When using multiple different players, it may come in handy to use a child object for placing the interaction controller. E.g. create an empty game object on every player combatant and place it where the interaction controller should be. Use this in the On Child setting (e.g. path/to/child).
Alternatively, you can also add the interaction controller object directly to the prefab of your player, but you need to disable Add Automatically.
Keep in mind
Every object that should be recognized by the Interaction Controller also needs a Collider component! Otherwise the trigger of the interaction controller will never receive OnTriggerEnter or OnTriggerExit events.
When testing your game, you can see the number of available interactions in the inspector of the Interaction Controller.
If your interaction controller seems to be pushed away from your player, this could be happening because there also is a Rigidbody component on your player. To solve this, enabler Is Kinematic on the interaction controller.
Also, make sure the interaction controller’s collider is as Is Trigger enabled.