Search Unity

get touch down?

Discussion in 'Scripting' started by TheRaider, Feb 21, 2013.

  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I have been making an iOS app and been using mousebuttondown for my raycasts which works fine. However if someone accidently has an extra finger on the screen it no longer works. Is it possible to handle multiple touches which a raycast easily?

    Thanks
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    There must be an easy way to do this!
     
  3. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Can you post your code you are using for this? You might need to run your touch functions in a for loop but not 100% sure what you have.
     
  4. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    Perhaps something like this?

    Code (csharp):
    1. for (var i : i in Input.touches) {
    2.     if (i.phase == TouchPhase.Began) {
    3.         raycast();
    4.     }
    5. }
    I'm not at my home computer so no way to test, sorry.
     
  5. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Code (csharp):
    1.  
    2.     if (Input.GetMouseButtonDown(0))
    3.     {
    4.         //empty RaycastHit object which raycast puts the hit details into
    5.         var hit : RaycastHit;
    6.         //ray shooting out of the camera from where the mouse is
    7.         var ray : Ray = cameraInterface.ScreenPointToRay(Input.mousePosition);
    8.  
    9.         if (Physics.Raycast(ray, hit))
    10.  

    thanks for the suggestion, i will give it a go. I know I need to convert to unityscript but I will give it a go :)
     
  6. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    Im ten years later but try this
    Code (CSharp):
    1. if (Input.touchCount > 0 && Input.GetMouseButtonDown(0))
    2. {
    3.     raycast();
    4. }
     
  7. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    but that wont work for multi touch
     
  8. JustHallowed

    JustHallowed

    Joined:
    Jan 22, 2021
    Posts:
    5
    This should do the trick

    Code (CSharp):
    1. if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    2. {
    3. // Do something
    4. }
     
    MaLiKeFTy97 and Shikamounty like this.
  9. MaLiKeFTy97

    MaLiKeFTy97

    Joined:
    Dec 13, 2021
    Posts:
    1
    this is perfect thanks !