Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Input.GetTouch Triggering Twice

Discussion in 'Scripting' started by rab236, Dec 7, 2011.

  1. rab236

    rab236

    Joined:
    Jan 17, 2009
    Posts:
    2,311
    Hi all. I'm having a problem with Input.GetTouch. As you can see by the title, it triggers twice every time the screen is touched. Here is my script:
    Code (csharp):
    1.  
    2. function Update () {
    3.     for (var i = 0; i < Input.touchCount; ++i) {
    4.         if (Input.GetTouch(i).phase == TouchPhase.Began) {
    5.             var hit : RaycastHit;
    6.             var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
    7.             if (Physics.Raycast (ray, hit)) {
    8.             hit.transform.gameObject.SendMessage("OnMouseDown", SendMessageOptions.DontRequireReceiver);
    9.           }
    10.        }
    11.    }
    12. }
    13.  
    What can I do to fix this and have Input.GetTouch trigger only once? All help is appreciated.
    Thanks.
     
  2. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    Does 'Began == Touched in the last update' - if that's not the case then it would explain this as the state could be Began for several updates.

    Often when I get double triggers it's becuase I've done something silly like attach the script twice.
     
    sijingsijinglin likes this.
  3. rab236

    rab236

    Joined:
    Jan 17, 2009
    Posts:
    2,311
    Thank you for your advice, but I believe Began is only called during the frame the finger touches the screen, so it should only be called once every touch. Also, unfortunately, the problem was not a careless error like attaching the script twice. :(
     
  4. Tasarran

    Tasarran

    Joined:
    Jan 20, 2011
    Posts:
    327
    Set a short delay at the beginning of your touch event handling, and enclose it in an if statement that checks to see if that delay has expired...

    pseudo code:

    Code (csharp):
    1.  
    2.  
    3. if (Time.time >= delay)
    4. {
    5.  delay = Time.time+.1;
    6.  do stuff here;
    7. }
    8.  
     
  5. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    What is the debug output if you add the following.

    Code (csharp):
    1. function Update () {
    2.     Debug.Print("Update Starting");
    3.     for (var i = 0; i < Input.touchCount; ++i) {
    4.         if (Input.GetTouch(i).phase == TouchPhase.Began) {
    5.             var hit : RaycastHit;
    6.             var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
    7.             if (Physics.Raycast (ray, hit)) {
    8.             Debug.Print(string.format("Input count = {0}", i));
    9.             hit.transform.gameObject.SendMessage("OnMouseDown", SendMessageOptions.DontRequireReceiver);
    10.           }
    11.        }
    12.    }
    13. }
     
  6. rab236

    rab236

    Joined:
    Jan 17, 2009
    Posts:
    2,311
    Debug.Print doesn't exist so I changed both of them to Debug.Log. I also capitalized String.Format. The only debug messages I got were "Update Starting." I didn't get any others, even when tapping the screen.
     
  7. rab236

    rab236

    Joined:
    Jan 17, 2009
    Posts:
    2,311
    @Tessaran
    I tried something like that already and it broke the script.
     
  8. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    Sorry for the bad formatting of the Debug statements - I wasn't infront of an editor when I put it together and should have mentioned that.

    If the input count string is never coming out then what is being triggered twice? You shouldn't be able to get to the SendMessage function without passing through that?
     
  9. rab236

    rab236

    Joined:
    Jan 17, 2009
    Posts:
    2,311
    I figured out the problem. :D I've been using Unity Remote on my iPhone, but that just acts the same as using a mouse, so having the OnMouseDown function called both when tapping and from the raycast script caused it to be triggered twice. Would it be called after exporting the app, or would I need to use the raycast script?
     
  10. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    I've found that the iPhone does respond to mouse events but they can't be trusted. For example the documentation says that OnMouseDown has no effect on the iPhone but I have found it does.
     
  11. sijingsijinglin

    sijingsijinglin

    Joined:
    Jun 8, 2018
    Posts:
    2
    On my, that's also my silly problem. Cannot believe this.