Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Touch Not Registering

Discussion in 'Scripting' started by Nigey, Sep 29, 2013.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi guys,

    First post ever, so bear with me.

    I'm trying to use mobile touch and it's just not working. I've attached my code to the plane, and it's just not recognizing anything...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TouchLogic : MonoBehaviour
    6. {
    7.     public static int currentTouch = 0;
    8.  
    9.     void Start ()
    10.     {
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if(Input.touches.Length <= 0)
    18.         {
    19.             return;
    20.         }
    21.         else
    22.         {
    23.             for(int i = 0; i < Input.touchCount; i++)
    24.             {
    25.                 currentTouch = i;
    26.                
    27.                 Debug.Log ("Input Touch Success");
    28.                 Debug.Log (Input.simulateMouseWithTouches);
    29.                 Debug.Log (Input.GetTouch(i).phase);
    30.                
    31.                 if(Input.GetTouch(i).phase == TouchPhase.Began)
    32.                 {
    33.                     Debug.Log("Yeah Touch has Begun!");
    34.                     return;
    35.                 }
    36.             }          
    37.         }
    38.     }
    39.  
    Literally none of the debugs ever come up.. I'm using the mouse right now to test this, as this is my first go on Unity.. I'm wondering if there's any invisible hoops I need to jump through.

    Been to build settings and tried setting to Android, Iphone, ect. Triedf adding inherant mobile scripts to my project from Unity.. Tried attaching those built in scripts (all of them even), to that same object.. Still no joy.

    I am completely racking my head with what the problem is.. Because technically it looks okay. The scene is nothing more than a orthographic camera and the plane, with the code attached to the plane.

    Can someone please help?!!?

    Thanks!!
     
    Last edited: Sep 30, 2013
    FreshCorduroy likes this.
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay firstly a shameless bump.. Secondly my progress thus far.

    Okay in failing to get any touch input and being unable to see the problem. I've gone as far as downloading the Android SDK and trying it on my Galaxy Note.. Still no joy (though it's cool to see my game on the device).

    Total noob here. Someone please throw me a bone.
     
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    okay going further than this I tried using the example code they have on the Unity Script Reference:

    http://docs.unity3d.com/Documentation/ScriptReference/Input-touches.html

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Example : MonoBehaviour {
    6.     void Update() {
    7.         int fingerCount = 0;
    8.         foreach (Touch touch in Input.touches) {
    9.             if (touch.phase != TouchPhase.Ended  touch.phase != TouchPhase.Canceled)
    10.                 fingerCount++;
    11.            
    12.         }
    13.         if (fingerCount > 0)
    14.             print("User has " + fingerCount + " finger(s) touching the screen");
    15.        
    16.     }
    17. }
    18.  
    Still nothing.. What am I doing wrong?

    I also checked in the debug log:

    Code (csharp):
    1.  
    2.     void Update() {
    3.         int fingerCount = 0;
    4.         foreach (Touch touch in Input.touches) {
    5.             if (touch.phase != TouchPhase.Ended  touch.phase != TouchPhase.Canceled)
    6.                 fingerCount++;
    7.            
    8.         }
    9.         if (fingerCount > 0)
    10.             print("User has " + fingerCount + " finger(s) touching the screen");
    11.         else
    12.         {
    13.             Debug.Log("Here: " + Input.simulateMouseWithTouches);
    14.         }
    15.        
    16.     }
    17. }
    18.  
    It's coming up as true.. So it's simulating mouse touches.. but not acknowledging the touches.. Any clue?
     
    Last edited: Sep 30, 2013
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay my next test in my inner monologue. Otherwise known as the forum..:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class KeyCodeBehaviour : MonoBehaviour {
    6.    
    7.     public float moveSpeed = 10f;
    8.     public float turnSpeed = 50f;
    9.    
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         Random.seed = (int)Time.time;
    14.     }
    15.    
    16.     void OnMouseDown ()
    17.     {
    18.         transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime);
    19.     }
    20.    
    21.     void Update ()
    22.     {
    23.         if(Input.GetKey(KeyCode.UpArrow))
    24.             transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime);
    25.        
    26.         if(Input.GetKey(KeyCode.DownArrow))
    27.             transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
    28.        
    29.         if(Input.GetKey(KeyCode.LeftArrow))
    30.             transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
    31.        
    32.         if(Input.GetKey(KeyCode.RightArrow))
    33.             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    34.     }
    35. }
    36.  
    This works in the editor when I press any of the buttons.. The cube moves around.. However on press the mouse or holding it.. There is no effect... That is pressing the mouse anywhere. So it seems like it's not even recognising mouse input.. Can anyone help with what on earth is going on??????????
     
    FreshCorduroy and summerian like this.
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Got it working. Don't worry. Took me about 20 hours tho lol.

    For anyone who wants to know. Use the most recent example. The youtube tutorials are out of date and you can't use them. Using this:

    Code (csharp):
    1.  
    2. public class MainTouchLogic : MonoBehaviour
    3. {
    4.     public float moveSpeed = 10f;
    5.     public float turnSpeed = 50f;
    6.  
    7.     void Start ()
    8.     {  
    9.         Input.simulateMouseWithTouches = true;
    10.     }
    11.    
    12.     void Update()
    13.     {
    14.         int fingerCount = 0;
    15.         foreach (Touch touch in Input.touches)
    16.         {
    17.             if (touch.phase != TouchPhase.Ended  touch.phase != TouchPhase.Canceled)
    18.             {
    19.                 fingerCount++;
    20.            
    21.             }
    22.             if (fingerCount > 0)
    23.             {
    24.                 transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime);
    25.             }
    26.                 else
    27.             {
    28.                 return;
    29.             }
    30.         }
    31.     }
    32. }
    33.  
    does the job.
     
    Last edited: Sep 30, 2013
  6. kamil.slawicki

    kamil.slawicki

    Joined:
    Jun 28, 2011
    Posts:
    8
    Hi there,

    I've tried your code and it does not work for me at all. Do you need to enable any options in the editor as well? I'm on Windows if that makes any difference.

    Thanks.
     
  7. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Are you trying on device?

    If you're trying on device it should work absolutely fine... I've followed through on that code and made this now:

    Code (csharp):
    1.  
    2.     // Update is called once per frame
    3.     void Update ()
    4.     {
    5.         int fingerCount = 0;
    6.        
    7.         foreach (Touch touch in Input.touches)
    8.         {
    9.             if (touch.phase != TouchPhase.Ended  touch.phase != TouchPhase.Canceled)
    10.             {
    11.                 fingerCount++;
    12.                
    13.                 if(fingerCount == 1  touch.phase == TouchPhase.Began) // If you want you can remove the 'Raycast section from here...
    14.                 {
    15.                     initTouchPos = touch.position;
    16.                    
    17.                     Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    18.                     RaycastHit hit;
    19.                    
    20.                     if (Physics.Raycast(ray, out hit))
    21.                     {
    22.                         if(hit.transform.tag == "ButtonPush" || hit.transform.tag == "ButtonPadAbove" || hit.transform.tag == "ButtonPadBelow" || hit.transform.tag == "ButtonShield")
    23.                         {
    24.                             hit.collider.GetComponent<ButtonScript>().Pressed();
    25.                         }
    26.                         else if(hit.transform.tag == "MoveBar")
    27.                         {
    28.                             float moveForce = (transform.position.y - touch.position.y);
    29.                             move.MoveIt(moveForce);
    30.                         }
    31.                     }
    32.                 } // To here....
    33.                
    34.                 /*if(fingerCount == 1  touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended)
    35.                 {
    36.                     Vector2 touchFacing = (initTouchPos - touch.position).normalized;
    37.                    
    38.                     if(Vector2.Dot(touchFacing, Vector2.up) > 0.8  Vector2.Distance(initTouchPos, touch.position) > 20)
    39.                     {
    40.                         Ability("SwipeDown");
    41.                     }
    42.                     if(Vector2.Dot(touchFacing, -Vector2.up) > 0.8  Vector2.Distance(initTouchPos, touch.position) > 20)
    43.                     {
    44.                         Ability("SwipeUp");
    45.                     }
    46.                     if(Vector2.Dot(touchFacing, Vector2.right) > 0.8  Vector2.Distance(initTouchPos, touch.position) > 20)
    47.                     {
    48.                         Ability("SwipeLeft");
    49.                     }
    50.                     if(Vector2.Dot(touchFacing, -Vector2.right) > 0.8  Vector2.Distance(initTouchPos, touch.position) > 20)
    51.                     {
    52.                         Ability("SwipeRight");
    53.                     }
    54.                 }*/
    55.             }
    56.         }
    57.     }
    58.        
    59.  
    For reference this code works using Android devices that I'm testing on. I can't imagine IPhone or Windows phone needing any different code.

    If you're trying this in the editor it wont really work. The code only works on the mobile device :).

    The raycast section is referencing external classes and you can remove it as shown in comments and it'll still work fine.. As for the commented out area.. That's to recognise swiping in different directions. Feel free to use it :)

    Hope that helps..
     
  8. kamil.slawicki

    kamil.slawicki

    Joined:
    Jun 28, 2011
    Posts:
    8
    Ok I see. I was hoping I could get the single touch input to work in the Editor. I pretty much wanted to save myself some work and not having to maintain two code bases, one for the Editor on one for the iOS.

    Question:

    What does
    Code (csharp):
    1. Input.simulateMouseWithTouches = true;
    do?

    Thanks for your help.
     
  9. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    I thought it would do exactly what you're hoping. It's the only part of the Input Unity Reference manual that's blank.. So I'm guessing it's a function in Unity that isn't working?

    http://docs.unity3d.com/Documentation/ScriptReference/Input.html
     
  10. prakash.jadhavjp

    prakash.jadhavjp

    Joined:
    Mar 27, 2015
    Posts:
    3
    this code world for all platforms ,

    if (Input.GetButtonDown ("Fire1")) {

    mobjVecTouchFirstPos = mobjVecTouchLastPos = Input.mousePosition;
    Debug.Log (mobjVecTouchFirstPos.x + "x and y" + mobjVecTouchFirstPos.y + " z " + mobjVecTouchFirstPos.z);
    }

    if (Input.GetButtonUp ("Fire1")) {

    mobjVecTouchLastPos = Input.mousePosition;
    ThrowTheBall();
    Debug.Log (mobjVecTouchLastPos.x + "x and y" + mobjVecTouchLastPos.y + " z " + mobjVecTouchLastPos.z);

    }
     
  11. wildlo93

    wildlo93

    Joined:
    Jul 14, 2016
    Posts:
    1
    hey ,
    i am having a problem with the touch thing firstly , my touch is perfect on the computer , i have made a football flick game , my ball is also curving the problem is in the apk when i install it on my mobile its touch starts to misbehave , there is alot of delay no accuracy no curving nothing , is there something u could help me with
     
  12. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  13. NoScopingDude

    NoScopingDude

    Joined:
    Apr 7, 2016
    Posts:
    7
    I use:
    if (Input.GetTouch(0).phase == TouchPhase.Began)

    Instead. NOTE: I don't use void update
     
  14. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Wow this was over 4 years ago, but better late than never :p lol. Such nostalgia.
     
  15. NoScopingDude

    NoScopingDude

    Joined:
    Apr 7, 2016
    Posts:
    7
    Sorry I am used to old posts being locked in forums
     
  16. Azern

    Azern

    Joined:
    Apr 11, 2017
    Posts:
    1
    Okay, maybe it's rude to revive this thread, but I want to see if I got a mistake in my code.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class clickbutton : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         Input.simulateMouseWithTouches = true;
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    15.         {
    16.             Debug.Log("is touched");
    17.         }
    18.     }
    19. }
    20.  
    I attach this code to a sprite and hope that when I click the sprite, I'll get the debug message. But it didn't.
    Anyone know why?