Adding Air Tap on speaker

In this project, we will be using the left-side speaker for applying Air Tap on speaker. However, you can apply the same for the right-side speaker as well.

Similar to Lenses, we have two objects here which we need to identify from the object explorer.

  • Navigate to Left_speaker_geo and left_speaker_details_geo in Object Hierarchy window
  • Tag them as leftspeaker and speakerDetails respectively

By default, when you are just viewing the Holograms, we will be hiding the speaker details section. This section only becomes visible when we do the Air Tap, and goes back again when we Air Tap again:

Speaker with Box Collider
  • Add a new script inside the Scripts folder, and name it ShowHideBehaviour. This script will handle the Show and Hide behaviour of the speakerDetails Game Object.

Use the following script inside the ShowHideBehaviour.cs file. This script we can use for any other object to show or hide.

public class ShowHideBehaviour : MonoBehaviour 
{
public GameObject showHideObject;
public bool showhide = false;
private void Start()
{
try
{
MeshRenderer render =
showHideObject.GetComponentInChildren<MeshRenderer>();
if (render != null)
{
render.enabled = showhide;
}
}
catch (System.Exception)
{
}
}
}

The script finds the MeshRenderer component from the gameObject and enables or disables it based on the showhide property. In this script, showhide is property exposed as public, so that you can provide the reference of the object from the Unity scene itself.

Attach ShowHideBehaviour.cs as components in speakerDetails tagged object. Then drag and drop the object in the showhide property section. This just takes the reference for the current speaker details objects and will hide the object in the first instance.

Attach show-hide script to the object

By default, it is unchecked, showhide is set to false and it will be hidden from view. At this point in time, you must check the left_speaker_details_geo on, as we are now handling visibility using code.

Now, during the Air Tapped event handler, we can handle the render object to enable visibility.

  1. Add a new script by navigating from the context menu Create | C# Scripts, and name it SpeakerGestureHandler.
  2. Open the script file in Visual Studio.

 

  1. Similar to SpeakerGestureHandler, by default, the SpeakerGestureHandler class will be inherited from the MonoBehaviour. In the next step, implement the InputClickHandler interface in the SpeakerGestureHandler class. This interface implement the methods OnInputClicked() that invoke on click input. So, whenever you do an Air Tap gesture, this method is invoked.
   RaycastHit hit;
bool isTapped = false;
public void OnInputClicked(InputEventData eventData)
{
hit = GazeManager.Instance.HitInfo;
if (hit.transform.gameObject != null)
{
isTapped = !isTapped;
var lftSpeaker = GameObject.FindWithTag("LeftSpeaker");
var lftSpeakerDetails =
GameObject.FindWithTag("speakerDetails");
MeshRenderer render =
lftSpeakerDetails.GetComponentInChildren
<MeshRenderer>();

if (isTapped)
{
lftSpeaker.transform.Translate(0.0f, -1.0f *
Time.deltaTime, 0.0f);
render.enabled = true;
}
else
{
lftSpeaker.transform.Translate(0.0f, 1.0f *
Time.deltaTime, 0.0f);
render.enabled = false;
}
}
}

When it is gazed, we find the Game Object for both LeftSpeaker and speakerDetails by the tag name. For the LeftSpeaker object, we are applying transformation based on tapped or not tapped, which worked like what we did for lenses. In the case of speaker details object, we have also taken the reference of MeshRenderer to make it's visibility true and false based on the Air Tap. Attach the SpeakerGestureHandler class with leftspeaker Game Object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.170.187