Unity provides an great event system which can be used to interact with a scene. This solution is particularly useful for UI interfaces. Humanoid Control provide extensive and flexible support for the event system using the Interaction module.
The interaction module is supported for gazing/pointing and finger touching. This means that you can trigger events by looking at objects, pointing at them or by touching them. You can determine yourself which body part is used for the gazing/pointing.
Note that you currently can use only one interaction module at the same time!
For head gaze, Humanoid Control provides a useful prefab called Gaze Interaction found in Humanoid/Prefabs/Interaction. This prefab should be placed on the First Person Camera, which is usually found as a child of the Head Target.
This prefab contains a preset Ray Interaction script and a default sphere focus object. The focus object shows what the player is currently looking at. It has set Timed Click to 2 seconds: objects will therefore received a PointerClick event when the user is looking at them for 2 seconds.
For more information on the Ray Interaction script, see below.
For finger pointing, two prefabs are provided in the Humanoid/Prefabs/Interaction map: Pointing Interaction (Left/Right Hand). These are meant to be placed on the Left and Right Hand Targets and are specifically tuned to be used with the default avatar, but you can adjust this easily.
The prefabs contain a preset Ray Interaction script and a Focus Point child object containing a line renderer showing to where the player is pointing.
The prefab also contains a Hand Input script with the Hand Pose Pointing set to the Activation of the Ray Interaction. Thus if the user is pointing with the hand, the Ray Interaction will be activated. It also sets the Controller Input‘s Trigger 1 to the Click function of the Ray Interaction script. Objects will therefore receive the PointerClick event when a player activated the Trigger 1 button.
Ray Interaction script
The ray interaction script implements interaction using a straight ray between the location of the Tansform and a focus point. The focus point is determined by casting a ray from the transform in the forward direction until a collider is hit. This hit point will be the location of the focus point. The focus point is only active when the active switch is enabled.
If the Timed Click is set to a non-zero value an object will receive a PointerClick event when the ray is pointed to the same object for the specified number of seconds.
You can design you own focus object which will be placed at the location where the player is currently looking. The object which should be used for that is set using the Focus Point Object parameter. Note: do not put a collider on the focus point object!
Finally, the Focus on Object parameter is read-only. When the ray is pointing to an object, this field will contain a reference to this object.
The ray interaction scripts supports the following function which can be called using Controller or Hand Input or by scripting:
|Click (bool)||will send a PointerClick event to the Object in Focus. When the bool is true||it send a PointerDown||when it is false||it sends a PointerUp event.|
|Activation(bool)||will turn the ray on or off.|
The ray interaction script supports the following events:
|PointerEnter||when the ray hits an object for the first time|
|PointerExit||when the ray no longer hits the object|
|PointerDown||when Click(true) is called while the ray hits an object|
|PointerUp||when the Click(false) is called while the ray hits an object or when the ray no longer hits the object|
|PointerClick||when the Click function is called while the ray is on an object|