Showing results for 
Search instead for 
Did you mean: 
Honored Contributor III

Unity Tip: Building A RealSense Powered Slider-Based Script Trigger

Hi everyone,

Many of you who have been using RealSense with Unity for a while will already be familiar with methods of specifying when a RealSense Action script or a non-RealSense script will be triggered,, such as Real World and Virtual World boxes, and SendMessageAction scripts.  There is however an additional method that you may not have considered yet: a RealSense-powered slider.


The principle is simple.  A slider block with a RealSense TrackingAction script inside it travels in two directions for a limited distance until it makes contact with a block at either end of the "cage" shaft.  These blocks contain an OnTrigger type non-RealSense script that is programmed to activate a particular event in that script whenever the slider touches the block.  

Such actions could include fire-once actions (making an avatar jump or side-step) or a looped action that continues until the slider moves away from the block's surface (e.g making an avatar walk and turn to change direction.)  Below, we provide a guide to making one of these cages for yourself.


Create a four-sized box from individual pieces.  Design the shape and height of your box to fit the direction that you want the slider to travel in and for how long before a trigger block is reached.

For example, if you want the slider to reach the block after a small up or down hand or face movement, use a short vertical cage.  If you want the hand or face to have to travel for a long distance before the event triggers, use a tall cage.  And if you want the event to be triggered by a left-right movement instead of a vertical one, build a horizontal cage for your slider.



Create a cube inside the cage to act as a slider.  Do not make it too thick, otherwise it will make contact with the trigger block prematurely.  Also leave a little empty space on either side of it so that its movement is not potentially impeded by friction caused by rubbing against the cage walls.



Place a TrackingAction script inside the slider block.  Leave it on its default 'Real World Box Values' and set the type of tracking (hand or face) that you prefer.  Also, give it a Weight value that reflects how "heavy" you want the slider to be (i.e how long it will take the slider to reach the trigger block when the defined tracking point is detected as being active.)

In a RealSense-powered slider block, the most important settings are the Virtual World Box and the movement Constraints.  These determine how far the slider can travel in the cage shaft before it stops, and what directions (up / down or left / right) it can travel in.


In the configuration above, the TrackingAction in the slider has its Constraints configured so that every position and rotation axis is locked (by the application of tick-marks in their boxes) except the 'Y' Position axis.  This tells Unity that the slider is only permitted to position-move vertically up and down, and not rotate at all.

The Virtual World Box values also reflect how we want the slider to travel.  The largest value will always be in the axis that the slider will travel along.  In a slider cage, a large number (even '1') causes a large movement.  In most cases, you therefore want to use a decimal number inbetween 0.1 and 0.9.  In our short-height format cage, '0.3' is sufficient for the slider to travel to either end of the shaft before being forced to stop moving.

You may notice that in the above configuration, the X and Z axes have Virtual World Box values of '0.1' set, even though those axes are not used because they are locked in the constraints.  This is just a personal preference, since if you have the value set to '0' then it changes automatically to '1' when the program is run.  A '0.1' setting prevents this automatic change and helps you to feel that you have more control over your own settings!

If you are using a taller cage, a Y-value of about '0.5' is recommended.  And if you are using a horizontal cage for left-right movement instead of a vertical one, you need to use the horizontal 'X' axis in your Virtual World Box and Constraint settings instead of the vertical 'Y'.  Also give 'X' a VWB value that reflects the horizontal width of the cage.  If it is a short shaft, use '0.3' for X.  If it is a lwide shaft, use a larger value such as '0.5'.


Usefully, once you have defined the Virtual World Box values then highlighting the slider from that point onward will show a red box around the object that visually describes very clearly the path that the object is allowed to take and how far it can move from its start point before it must stop.



Place a 'Rigidbody' physics component inside the slider block by going to the 'Component' menu option of Unity and selecting the sub-options 'Physics' and 'Rigidbody.'


Once the Rigidbody has been placed in the slider, give it the following settings in the Inspector panel:


If you are wondering why we have not set any constraints in the Rigidbody: the reason is that when an object's movement is powered by a TrackingAction script, it totally ignores the settings in a Rigidbody (unless you tick the 'Effect Physics' option at the base of the Rigidbody, which is usually not a good idea since the TrackingAction and Rigidbody don't get along with each other well.)  

Thepresence of the Rigidbody is actually for the benefit of the trigger mechanism in the trigger block's collider field - it usually needs to be touched by an object (e.g our slider) that is equipped with a Rigidbody in order for the trigger to activate.


Having set up our slider to move how we want it to, we now need to configure the trigger blocks at the ends of the shaft to do something when the collider field surrounding the slider block makes contact with the collider field of the trigger blocks.

To do this, we need to inform Unity that an event is supposed to be triggered when the trigger block's collider is interacted with.  This is accomplished by highlighting the trigger block and then going to its collider field settings in the Inspector panel and placing a tick-mark in the 'Is Trigger' option.  Then Unity knows to treat that particular collider field as a trigger mechanism that other objects can pass through instead of a physical stopping mechanism.


It is worth mentioning that you only need to make both of the trigger blocks at either end of the shaft an Is Trigger if you intend for something to happen when the slider touches either end of the shaft.  If you only want an event to trigger when the slider is moved in one direction then you only need to trigger-ize the block that is at that end of the shaft.  

This single-trigger methodology is particularly useful in short-height cages, because the short travel distance of the slider means that it can be easy to accidentally activate the down-trigger when you meant to activate the up-trigger, especially if the TrackingAction causes the slider to bounce around the shaft (something that is a common occurrence).  Putting the 'up' and 'down' triggers in two separate short cages can make the controls much more stable.




With our colliders set up to be triggers, it is now time to give the trigger blocks something to do when the trigger is activated.  We do this by placing an "OnTrigger" type script inside the trigger block/

In Unity, there are three types of common Trigger script: OnTriggerEnter, OnTriggerStay and OnTriggerExit.

- The OnTriggerEnter function tells Unity to activate the instructions in a script only when a collider field that is not set as a trigger enters its boundaries.

- OnTriggerStay runs the script on a loop in the style of an 'Update' type script function until the collider that triggered it is moved back outside of its boundaries, and then the script stops running.

- OnTriggerExit only activates the trigger once a collider has entered its boundaries and then exited again.

To make a script an OnTrigger, you simply need to replace the usual Function name (e.g Start or Update) with an OnTrigger instruction.  Thankfully for the sake of easy programming, the instruction is the same for both the JavaScript and C# languages.


function OnTriggerEnter (other: Collider) {
function OnTriggerStay (other: Collider) {
function OnTriggerExit (other: Collider) {


void OnTriggerEnter (other: Collider) {
void OnTriggerStay (other: Collider) {
void OnTriggerExit (other: Collider) {

In this JavaScript example, Unity switches on a specific light source when the trigger is activated:

function OnTriggerEnter (other: Collider) {

var go = GameObject.Find("Attic Light 3");


And in this C# example, a TrackingAction inside the same object as the trigger script is switched off when the trigger is entered.

using UnityEngine;
using System.Collections;

public class TrackingAction_Off_Collision : MonoBehaviour

    private MonoBehaviour ScriptOnOff;

    void OnTriggerEnter(Collider other)


        ScriptOnOff = GetComponent<TrackingAction>();

        ScriptOnOff.enabled = false;




By default, the collider field of the trigger block will be skin-tight against the block's surface, making it very difficult for the slider's collider to interact with that field and trigger it.  We therefore want to give the trigger block's field an "offset" so that it extends beyond the object surface and reaches out a little towards the slider.

Highlight a trigger block to bring up its collider settings in the Inspector and go to the 'Offset' section of those settings.  Place a small decimal value in one of the X, Y or Z boxes (the one to choose will likely vary depending on what direction the blcok is facing in your particular project) until you see that the collider has extended outwards a short distance.



The slider's collider will now be able to reach into the trigger block's collider and set it off.


Once your slider is set up and your trigger blocks are configured as triggers and have an OnTrigger type script in them, you can now run your program and test the trigger out!  

For your first tests, we recommend switching off the project's Main Camera and placing a new camera beside the slider cage instead so that you can observe how it moves when you use the hand or face control that you defined in the slider's TrackingAction.  You can then make tweaks to the various settings of the TrackingAction and trigger blocks (Virtual World Box, weighting, trigger collider offset, etc) until you get the slider interacting perfectly with the trigger block in precisely the way that you need it to.


Once you are happy with the settings, you can delete or disable the test camera (we usually prefer just turning it off so we can resume tweaking quickly if we need to without having to make another camera) and switch the Main Camera back on..The best way to toggle a camera on and off is to use the tick-box in the middle of its Inspector settings, not the tick-box at the very top.


If you have a lot of objects in your project, you can find cameras quickly by typing 'camera' into the search box at the top of the Hierarchy panel.


At this point, your slider cage can be considered completed.  However, you can take the design further to give it greater control accuracy if you wish.

For example, we mentioned earlier that the sliders have a tendency to bounce around the shaft in the short cages and can trigger an event multiple types as it bounces in and out of the trigger field repeatedly.  We have developed a method for minimizing this though.


Create a C# script with the following code:

using UnityEngine;
using System.Collections;

public class TrackingAction_Off_Collision_Timer : MonoBehaviour
	private MonoBehaviour ScriptOnOff;
	void OnTriggerEnter(Collider other)
		ScriptOnOff = GetComponent<TrackingAction> ();
		ScriptOnOff.enabled = false;

		StartCoroutine (PauseTracking ());   

	IEnumerator PauseTracking() {

		yield return new WaitForSeconds (1);

		ScriptOnOff = GetComponent<TrackingAction> ();
		ScriptOnOff.enabled = true;

In the third line, change 'TrackingAction_Off_Collision-Timer' to whatever name you gave the C# script when you created it.  Place the script inside your slider block where your TrackingAction script is.

If you are just starting out with learning C# and would like to understand better how the above script works, we have a detailed breakdown of a slightly older version of the scirpt (minus the on-off timer) in the article linked to here:

If you normally create JavaScript format scripts in your project, remember to select the 'CSharp' menu option on the script creator or you will end up creating a JavaScript script file, and the above code will not work in it.  Also remember to change the setting back to 'JavaScript' next time you make a new JavaScript file!



Highlight your slider block and go to the 'Component' Unity menu and select the sub-options 'Physics' and 'Box Collider' to add a secondary box collider field to your slider.



Set that secondary collider field to be an 'Is Trigger' field.


Now when your slider block makes contact with the trigger block's collider field, the secondary collider field of the slider - its trigger field - will be activated when it comes into contact with the collider field of the trigger block (a reversal of the normal activation process) and the C# script inside the slider will run.

What this script does is switch off the TrackingAction inside the slider - making the slider unable to move - and then switches it back on again after a period of 1 second to allow movement again.

By toggling the TrackingAction of the slider off for a certain period and then on again, you can help to prevent the trigger block from being activated multiple times if the slider is bouncing rapidly on and off the surface of the trigger block.

In our own project, we used this mechanism with our avatar's Jump action, which is activated by jerking the user's head upwards.  Up until now, if the slider was bouncing then the Jump action would fire multiple times per second and the avatar would be doing double-jumps and triple-jumps in mid-air until its head got stuck in the ceiling!

But with this timer mechanism, the TrackingAction switches off for 1 second after the first jump activation - negating the bouncing - and gives the avatar time to drop down to the floor at the end of the jump action before the TrackingAction in the slider switches back on again, ready for the next jump command to be issued by the user.

If you have any questions about the processes in this article, please feel free to ask them in the comments below.  Best of luck!

0 Kudos
0 Replies