Interactions
Last updated
Last updated
To define custom behavior when interacting with an object, first change the object's layer to Interact to indicate that the object is interactable.
Next, to specify the desired actions upon interacting with the object, attach a Custom Interact Event component to the object and configure events for when you Start, Hold, or Stop interacting with it.
Alternatively, you can implement an interfaces into your custom script and define unique behaviors without relying on events within the Custom Interact Event component.
You can use these types of interfaces to define your own behavior:
IHoverStart - The method is called when you hover the mouse over the interactive object.
IHoverEnd - The method is called when you stop hovering the mouse over the interactive object.
IInteractStart - The method will be called when you start interacting with the object, once.
IInteractHold - The method will be called as long as you keep the use button pressed.
IInteractStop - The method will be called when you stop interacting with the object, once.
IInteractStartPlayer - The method will be called once when you start interacting with the object, but it will receive a parameter containing a reference to the player object.
IStateInteract - When you interact with the object, the current state of the player's state machine will be changed based on the parameters used in the StateParams.
IInteractTimed - You will interact with the object when you hold down the use buttom button for a specified amount of time.
IInteractTitle - The title displayed when interacting with the object will be changed based on the parameters used in the TitleParams.
You can also implement interfaces that allow you to define custom behavior when you examine an object and that object has interactive elements such as switches, etc. (example: Radio)
IExamineClick - The method will be called when you click on the interactive object.
IExamineDragVertical - The method will be called when you click and move the mouse in the vertical direction. As an argument, you get the dragDelta value.
IExamineDragHorizontal - The method will be called when you click and move the mouse in the horizontal direction. As an argument, you get the dragDelta value.
To change the interact message displayed when you interact with an object, you can add a Custom Interact Title component to the object.
You can define whether the title Is Dynamic, which means it can change dynamically based on the reflected field or property value of the script.
The Reflected Name variable must be of type Bool, otherwise it will not be able to get a true/false value.
If a text starts with an asterisk (*), it will be treated as plain text rather than a key for localization.
To change the interact reticle when you interact with an object, you can add a Custom Interact Reticle component to the object.
You can override the interact reticle while holding the use button by enabling Dynamic Hold Reticle and defining the reflection variable that will determine when to switch to the hold reticle.
When you implement the IInteractTime interface in your custom script, you will be needed to modify the interface parameters a little bit.
At the top of the InteractTime parameter, add the [field: SerializeField]
attribute as following. This converts the property to an field and allows Unity to serialize it and display it in the inspector.
Then just define whether the interaction should be performed or not by setting the NoInteract value to false or true. In my case, I just created a new field that controls the interaction state.
Here is the whole script: