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.
- Select the + button in the ability list under the “Abilities” foldout of the Ultimate Character Locomotion component.
- Add the Interact ability.
- 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 components 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 Ability Start 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.
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.
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.
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.