Interact
The Interact ability is a versatile ability that allows the character to interface with another object within the scene. Examples include opening a door or pressing a button. This ability is designed to be used with any other object though so is not limited to just those two examples. The Interact ability is a child of the Detect Object Ability Base so will inherit any properties from it.
Setup
- Select the + button in the ability list under the “Abilities” foldout of the Ultimate Character Locomotion component.
- Add the Interact ability. On the Interact ability the Object Detection enum should be set to Trigger.
- Decide which object the ability should interact with. In this example we are going to use a moving platform: when the character interacts with the button it’ll start the moving platform. A moving platform is already setup for this example. Ensure the Moving Platform has Enable On Interact enabled.
- Add the Interactable component to the button. The Interactable component is responsible for performing the actual interaction. The Interact ability will look for this component when deciding if an object can be interacted with.
- The Interactable component now needs to know what object it should interact with. Set the Targets field to 2 so two targets can be specified. The first target will be the moving platform, and the second will be the button itself so the button press animation can be played.
- The moving platform should be specified as the first element within the Targets array. The second element should be the button and for that we need to add a new component: the Animated Interactable component. This component will play an animation when the object is interacted with. An Animator should also be added to the button so the Animated Interactable component can play the button press animation.
- Taking a look at the InteractButton Animator Controller we’ll see that the button press animation is started when the Press parameter is triggered. With this setup the Animated Interactable component should specify:
- Trigger Parameter: Press
- Trigger Parameter: Press
- Now both the moving platform and the button press Interactable Targets will respond to the interact, but the ability needs to know where the character should be positioned to actually press the button. For this we can add the Move Towards Location component to the button.
- For this example we want the button to be able to be interacted with when the character is within a trigger. Under theĀ Object Detection field of the Interact ability on the character we’ll set a value of Trigger. A trigger should now be added to the button that the ability will detect.
- The interact object is now ready to go! When the character enters the trigger the Interact ability will be able to start. When the ability is started it’ll interact with the Interactable component after the Interact Event has triggered. The ability will then complete after the Interact Complete Event has triggered.
API
IInteractableTarget
New objects can be interacted with by implementing the IInteractableTarget interface. With the IInteractableTarget the Interact/Interactable scripts do not need to be modified in order to support a new interactable object. The IInteractableTarget interface contains two methods that must be implemented:
/// <summary> /// Can the target be interacted with? /// </summary> /// <returns>True if the target can be interacted with.</returns> bool CanInteract(); /// <summary> /// Interact with the target. /// </summary> void Interact();
CanInteract returns a bool indicating if the object can be interacted with. An object may not be able to be interacted with if it has already been interacted with (such as a chest being opened) or is currently being interacted with (such as a moving platform that is in the process of moving). Interact will indicate that the interaction should start.
IInteractableMessage
When the character goes to interact with an object the ability can show a message indicating to the user that an interaction is possible. This message is set via the Ability Message Text field on the ability. For most abilities this text is static because the is only one type of interaction. However, with the Interact ability, the type of interaction can change based on the current game state.
Taking a look at the demo scene, the Interact ability that interacts with the fifth Interactable (the Open Door Interact ability) can change the ability message between “PRESS F TO OPEN DOOR” and “PRESS F TO CLOSE DOOR”. Notice the Ability Message Text has the string “PRESS F TO {0} DOOR”. The Interact ability will look for the IInteractableMessage interface on the Interactable object to display this string. String.Format is used so the Ability Message Text must contain “{0}” in order for the IInteractableMessage to appear.
The IInteractableMessage interface just contains one method that must be implemented in order for the Interact ability to display the text.
/// <summary> /// Returns the message that should be displayed when the object can be interacted with. /// </summary> /// <returns>The message that should be displayed when the object can be interacted with.</returns> string AbilityMessage();
ShouldBlockAbilityStart
The Interact ability overrides Ability.ShouldBlockAbilityStart (see New Ability) to prevent all Item Abilities from starting whilst an Interact ability is in progress, as well as any abilities with a lower priority.
Inspected Fields
Interactable ID
The ID of the Interactable. A value of -1 indicates no ID. This value is used when the character can interact with multiple objects. For example, in the demo scene the button has an Interactable ID of 1 and the chest has an ID of 2.
Ability Int Data Value
The value of the AbilityIntData animator parameter.
Interact Event
Specifies if the ability should wait for the OnAnimatorInteract animation event or wait the specified amount of time before interacting with the item.
Interact Complete Event
Specifies if the ability should wait for the OnAnimatorInteractComplete animation event or wait the specified amount of time before stopping the ability.