Search Unity

Is this the best way to simulate button taps?

Discussion in 'Scripting' started by elpuerco63, Apr 6, 2016.

  1. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I have 3D GameObjects that I use with Colliders as triggers and for the most part work OK, but am now trying to create a toggle on / off button on a 3D model. For this I cannot use the Unity UI.

    I have a BoxCollider on the GameObject acting as a button and attached this code:

    Code (csharp):
    1.  
    2. void Update () {
    3.  
    4.     if (Input.touchCount == 1) {
    5.  
    6.       Touch touch = Input.GetTouch (0);
    7.  
    8.       Ray ray = Camera.main.ScreenPointToRay (touch.position);
    9.  
    10.       RaycastHit hit;
    11.  
    12.       if (Physics.Raycast (ray, out hit) && (hit.collider.gameObject.name == "3DButton")) {
    13.  
    14.           butterHandler ();
    15.       }
    16.     }
    17.  }
    18.  
    But what is happening is the code gets called so fast it triggers a button tap multiple times on a single tap?

    I have tried using boolean flags but still no luck so started to wonder am I actually doing this the right way?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271