Documentation
Tutorials
Gesture Documentation
Related
Documentation
Tutorials
Gesture Documentation
Related
This is an old revision of the document!
In this lesson, we will be building on the interactive bitmap tutorial by creating a multitouch clock. We will learn how to make following items touchable in the scene:
Here is a snapshot of the finished clock tutorial in game mode:
Copy from your GestureWorks install location, usually C:\Program Files(x86)\Ideum\GestureWorks2, the Unity project UnityGestureWorksClock - Start. Open this folder with Unity and open the scene file Scenes/Clock.unity. Your screen should be similar to this:
If you look at the scene you'll see the clock is setup with colliders for the clock base, minute hand, and bells. These are the areas that will respond to gestures.
Our first task with the clock is to have the bells play a sound in resonse to tap. Create a new C# script called TouchBell. Be sure to inherit from TouchObject like the interactive bitmap example. The final script for TouchBell is as follows:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TouchBell : TouchObject { public void ntap (GestureWorks.GestureInfo gesture) { GetComponent<AudioSource>().Play(); } }
Click and drag TouchBell to the Bell_L and Bell_R object in the Hierarchy panel. Configure the attached script for each bell to support 1 gesture and type in ntap for Element 0.
Test out what you have done so far to ensure that the bells ring and that there are no errors. If you don’t hear anything be sure your audio connection is working correctly and check the output log file to see if there were any errors loading files.
We will now add the clock script. Make a new C# script and name it TouchClock. Put the following code in the file:
using System.Collections.Generic; using UnityEngine; public class TouchClock : TouchObject { public void ndrag(GestureWorks.GestureInfo gesture) { Camera cam = Camera.main; Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam); Plane plane = new Plane(planes[5].normal, gameObject.transform.position); float dX = gesture.value("drag_dx") * Screen.width; float dY = gesture.value("drag_dy") * Flipped * Screen.height; if(dX == 0.0f && dY == 0.0f) { return; } if(gesture.x == 0.0f && gesture.y == 0.0f) { return; } float screenX = gesture.x; float screenY = Screen.height - gesture.y; Ray dragRay = cam.ScreenPointToRay(new Vector3(screenX, screenY, 0.0f)); float dragEnter; if(!plane.Raycast(dragRay, out dragEnter)) { return; } Vector3 newPosition = dragRay.origin + dragRay.direction * dragEnter; Ray prevRay = cam.ScreenPointToRay(new Vector3(screenX - dX, screenY - dY, 0.0f)); float prevEnter; if(!plane.Raycast(prevRay, out prevEnter)) { return; } Vector3 prevPosition = prevRay.origin + prevRay.direction * prevEnter; transform.position += (newPosition - prevPosition); if (transform.position.y < 0.0f) { transform.position = new Vector3(transform.position.x, 0.0f, transform.position.z); } } public void nrotate(GestureWorks.GestureInfo gesture) { float multiplier = 0.75f; Camera cam = Camera.main; float dTheta = gesture.value("rotate_dtheta") * multiplier; transform.Rotate(0, 0, dTheta); } public void nscale(GestureWorks.GestureInfo gesture) { float scaleDX = gesture.value("scale_dsx") * Screen.width; float scaleDY = gesture.value("scale_dsy") * Screen.height; float scale = (scaleDX + scaleDY) * 0.5f; float cx = Mathf.Clamp(transform.localScale.x + scale, 0.5f, 2f); float cy = Mathf.Clamp(transform.localScale.y + scale, 0.5f, 2f); float cz = Mathf.Clamp(transform.localScale.z + scale, 0.5f, 2f); transform.localScale = new Vector3(cx, cy, cz); } }