How to use Events in Grabbable component¶
In this tutorial, we will focus on how to call a function through an Event call, specifically using Hover Entered and Hover Exited.
Before we start, you should really check the events in the manual first.
Create a script; we will name it TestHover:
This is the implementation:
public class TestHover : MonoBehaviour
{
public void TestHoverEntered()
{
Debug.Log("Interactor is hovering on interactable!");
}
public void TestHoverExited()
{
Debug.Log("Interactor stopped hovering on interactable!");
}
}
The TestHoverEntered
function will test the Hover Entered event works by printing a message to the console when its triggered.
The TestHoverExited
function will test the Hover Exited event works by printing a message to the console when its triggered.
Create an empty game object and attach the TestHover script to it.
Open the Events of the Grabbable component and add a new element to the Hover Entered event.
In that new Element, make sure it’s Runtime only or Editor and Runtime.
Drag and drop the new empty game object you made in Step 2.
Click on “no function,” go to TestHover, and click TestHoverEntered, and you are done.
Repeat steps 3-5 for Hover Exited.
Now, if you run the simulation and hover the interactor (hand) over the object, the message “Interactor is hovering on interactable!” will be printed on the console.
If you stop hovering over the object, the message “Interactor stopped hovering on interactable!” will be printed.
The same can be applied to the rest of the events like Select and Activate.