"Head Nod" - Trigger action for face
-
Hi Gio! Great question. I've created a script that checks to see if the user has nodded their head. Follow these steps to get it working.
- Create a New Project
- Add a Head Binding object (Objects Panel -> Add New -> Head Binding)
- Add a new empty object (Objects Panel -> Add New -> Empty Object)
- Create a new script (Resources Panel -> Add New -> Script)
- Attach this new script to the new empty object. One easy way to do this is to select the empty object and drag and drop the script onto it in the inspector panel
- Add the below code to the new script
- Then, in the inspector, set the Head Binding Object property to the Head Binding you created
- You'll now notice anytime you nod that "User Nodded" is printed out to the Logger
- Now, add any logic you want to have happen into the nodded() function
// -----JS CODE-----
// @input SceneObject headBindingObject
// @input int numNodSamples = 10
// @input float nodThreshold = 0.15
var nodSamples = [];
function update (eventData)
{
var headTransform = script.headBindingObject.getTransform();
var headForward = headTransform.forward;
var headForwardNoY = new vec3( headTransform.forward.x, 0.0, headTransform.forward.z )
var dot = headForwardNoY.dot( headForward );
if( nodSamples.length >= script.numNodSamples )
{
nodSamples.shift();
}
nodSamples.push( dot );
checkForNod();
}
var event = script.createEvent("UpdateEvent");
event.bind( update );
function checkForNod()
{
var latestNodIndex = nodSamples.length - 1;
var latestNod = nodSamples[latestNodIndex];
for( var i = latestNodIndex - 1; i > 0; i-- )
{
var diff = latestNod - nodSamples[i];
if( diff < -script.nodThreshold )
{
nodded();
nodSamples = [];
break;
}
}
}
function nodded()
{
print( "User Nodded" );
}Comment actions -
Hi Travis!... Thank you so much!.. Much appreciated!.. :)
Comment actions -
why is this function only triggered by head up?And head down didn't?
Comment actions
Please sign in to leave a comment.
Have a comment?