Eye Tracking
The Eye Tracking effect allows you to track the position and rotation of the user’s eyes. The Eye Tracking effect uses the Head Binding component to change the transform of the object the component is on.
Attaching Objects to the Eye
You can attach an object to the eye by making your object a child of an object with the Head Binding
component, with the Left
or Right
Eyeball option selected.
The Left Eye
and Right Eye
object will position and rotate themselves based on the user’s eye. You can add an object as a child of either object to have your object track the eye.
Eyeballs Example
You can see a usage of this technique in the Eyeballs
asset in the Asset Library.
In this asset, each eye will come with two objects: Sclera and Choroid. These objects are provided as an example and can be removed. The Sclera adds a reflective sphere around the Choroid. Both use the default PBR material.
Add Eyeballs
Once you've imported Eyeballs
from the Asset Library, you will find it in your Asset Browser
panel.
Next, drag the prefab inside the folder underneath the main camera, as per the instruction in its name. This will add a Left Eye
and Right Eye
object, each with their own Head Binding
component and their respective attach point.
Using the Eyeball Rotation Only
The Head Binding
component will modify both the rotation and position of the object it is attached to.
In some cases, you may only want to use the rotation and manually position the eyeball. For example, in the image below, the eye on the character shouldn't be positioned where the natural eye is.
One way we can do this, is by tracking the eyeball to the head, but NOT to the eye itself. For example: below we attached the two eyes object to the Face Center
.
Next, you might want to copy the rotation of the eye onto the head tracked object. You can use the following script to copy the rotation from one object to another.
// CopyRotation.js
// Version: 0.0.1
// Description: Copies rotation from one object to another
// @input SceneObject sourceObject
// @input SceneObject targetObject
var targetTransform;
var sourceTransform;
if (script.targetObject) {
targetTransform = script.targetObject.getTransform();
} else {
targetTransform = script.getSceneObject().getTransform();
}
if (script.sourceObject) {
sourceTransform = script.sourceObject.getTransform();
} else {
print('[CopyRotation] Source object is not set');
}
function onUpdate() {
if (sourceTransform) {
targetTransform.setWorldRotation(sourceTransform.getWorldRotation());
}
}
var updateEvent = script.createEvent('UpdateEvent');
updateEvent.bind(onUpdate);
With the script below selected, in the Inspector
panel, you can select the object with the Head Binding
component tracking the eye
as the source, and the object you want to rotate with the eye as the target.
You can checkout the Face Expressions example in the Asset Library to see the complete setup above.
Changing the Eye to Track
Like any object with the Head Binding
component, you can change the Face Index
field to change which face to track for the eyeballs. The first detected face is 0
, the second face is 1
, and so forth.