Is It Possible To Detect Touch Only On Certain Object?

Discussion in 'Buildbox 3.0' started by nyamuk91, Jan 13, 2019.

  1. nyamuk91

    nyamuk91 Boxer

    Joined:
    Aug 15, 2018
    Messages:
    95
    Likes Received:
    20
    If I use the built-in 'Touch' node, it detects touches from all over the screen instead of just detecting the touch on the object the node attached to. Having the object created in UI (in the form of a button) works but it doesn't feel right.

    Is there any way to limit the touch only to the object through javascript or node?
     
  2. Scriv

    Scriv Boxer

    Joined:
    Oct 28, 2018
    Messages:
    50
    Likes Received:
    21
    The code you need can be found in the pool preset. it takes the xy value of the object and gives it a perimeter of pixels. you can make that bigger or smaller in the code.
     
    Tom King and nyamuk91 like this.
  3. nyamuk91

    nyamuk91 Boxer

    Joined:
    Aug 15, 2018
    Messages:
    95
    Likes Received:
    20
    Thanks! Exactly what I'm looking for
     
  4. isac cabrera

    isac cabrera Boxer

    Joined:
    Nov 8, 2018
    Messages:
    50
    Likes Received:
    8
    Hi guys!! im trying to find this option, i am not an avid programmer so i am almost guessing where to find it but, is here where i must edit the code??

    let wp = this.scene().screenToWorld(point.x, point.y); ????????????????

    if this is so what should i do?
     
  5. nyamuk91

    nyamuk91 Boxer

    Joined:
    Aug 15, 2018
    Messages:
    95
    Likes Received:
    20
    Hi bro. You can refer to this API docs:
    http://download.buildbox.com/buildb...-component-script-enabletouchswallow-priority

    Basically, the touchBegan(point) is a built-in function that will detect user touch on the screen. The function will receive the location of the click via point parameter. Inside this function, you then need to add a check to see if the point variable is at the same position with the object that you want the touch to be detected. However, the point variable and object position uses 2 different unit so you need to convert them and then measure the distance.

    You can do something like this:
    Code:
    function touchBegan( point ){
       let _startPoint = point;
       let hitbox_size = 0.5;
      
       var wp = this.scene().screenToWorld(_startPoint.x, _startPoint.y);
       wp = new Vec3(wp.x, wp.y, wp.z);
       pos = new Vec3(pos.x, pos.y, pos.z);
    
       // Example if you are measuring in the Y axis. Change accordingly
       if((wp.y-pos.y<= hitbox_size) && (wp.y-pos.y>= -hitbox_size)){
          log("Touched!")
          this.emitSignal('Touch', true);
       }
    }
    component.touchBegan = touchBegan;
    
     
    Last edited: Jan 18, 2019
    Pull Tea, weboha and isac cabrera like this.
  6. isac cabrera

    isac cabrera Boxer

    Joined:
    Nov 8, 2018
    Messages:
    50
    Likes Received:
    8

    This is great man!!! Thanks a lot!!
     
  7. jmp909

    jmp909 Boxer

    Joined:
    Feb 2, 2020
    Messages:
    28
    Likes Received:
    4
    In BuildBox 3, the IsTouched node will pass a “true” value in the signal for the specific object
     
    Sean Buildbox and Borteist like this.
  8. Borteist

    Borteist Boxer

    Joined:
    Mar 2, 2020
    Messages:
    1
    Likes Received:
    0
    You are a lifesaver
     

Share This Page