Touch Trigger Script to allow natural touch of buttons


Some users complain about the LeapMotion implementation on HighFidelity. The LeapMotion it is working perfectly, but need some proper interaction scripts in order to be used. I want to share a script that can be used for a natural interaction with buttons, using your controllers, LeapMotion or even desktop mode ( by pressing the X key ). It must be placed as an entity script on a hidden box that covers your button.

(function() {
	var clicked = false;
    var _this;

    DetectTouched = function() { 
         _this = this;

    DetectTouched.prototype = {

        update: function() {
            var entityID = _this.entityID;
            var leftHandPosition = MyAvatar.getLeftPalmPosition();
            var rightHandPosition = MyAvatar.getRightPalmPosition();
            var props = Entities.getEntityProperties(entityID);
            var entityMinPoint = props.boundingBox.brn;
            var entityMaxPoint = props.boundingBox.tfl;

            if (pointInExtents(leftHandPosition, entityMinPoint, entityMaxPoint) || pointInExtents(rightHandPosition, entityMinPoint, entityMaxPoint)) {

                _this.beingTouched = true;
                print("I'm being touched...");
				if (clicked === false){
				 clicked = true;

            } else if (_this.beingTouched) {
                _this.beingTouched = false;
				clicked = false;
                print("I'm am no longer being touched...");

        preload: function(entityID) {
            this.entityID = entityID;
			//here you can also read the entity or the parrent User Data if you need some values for your triggered action

        unload: function(entityID) {

    return new DetectTouched();

Yo can use it in parallel with the classic click and near/far trigger scripts, to cover better all the posible interactions of the user with the button.

Happy touching :slight_smile: