Touch Only One - Touch Closest to Camera

If you having problem of touching just one item in your Buildbox game then this is for you.

Closest to Camera Node

Downloads:
OR

1) Create a new Script node, name it "Closest to Camera".

2) Add 1 input "Touch" and 1 output "Touched".

3) Connect the output of "Is Touched" node to input of "Closest to Camera" node.

4) Connect the output of "Closest to Camera" node to input of "Touch Move" node or some other node that you want to control.

Closest to Camera Node

Copy the fallowing code into nodes script.
let ent;
  let cam;
  let touched = false;
  let delay = .01;// time to wait for all the touch events to run
  let time = 0;
  
  function init() {
    ent = this.entity();
    cam = ent.camera();
  }
  
  function update(dt) {
    if (touched) {
      if(time<delay){
        time+=dt;
        return;
      }
      touched = false;
      time=0;
      if (Settings.closestObj.ent == ent) {
        Settings.closestObj = undefined;
        this.emitSignal("Touched", true);
      }
    }
  }
  
  function signal(name, value) {
  
    if (value) {
      let dist = ent.position().distance(cam.screenRay(new Vec3(0,0,0)).origin);
      closest(ent, dist);
  
    } else {
      touched = false;
      this.emitSignal("Touched", false);
    }
  }
  
  function closest(ent, dist) {
    if (Settings.closestObj === undefined) {
      Settings.closestObj = {};
      Settings.closestObj.ent = ent;
      Settings.closestObj.dist = dist;
    } else if (Settings.closestObj.dist > dist) {
      Settings.closestObj.ent = ent;
      Settings.closestObj.dist = dist;
    } else {
      touched = false;
      return;
    }
    time = 0;
    touched = true;
  }