Search Unity

iPhoneTouch.position ... an example, I beg you! :-)

Discussion in 'iOS and tvOS' started by DaveyJJ, Oct 27, 2008.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Can anyone, anyone at all, please provide a simple script example of how to use iPhoneTouch.position? Pretty please with sugar on it?
     
  2. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    First a little explanation: the tricky thing about iPhoneTouch.position is that you can't just use it like that. You have to get an iPhoneTouch-Object out of iPhoneInput.touches...

    Just put the following script on an empty GameObject or on the Camera, add 5 primitives (or whatever) to the scene and assign one to each element of the Array "thing".

    Now if you run the scene, the first finger to touch the screen will move the first object, the second the second object, etc.

    Hope this helps! :]


    Code (csharp):
    1. var thing = new GameObject[5];
    2. var line = new String[5];
    3.  
    4. function Update () {
    5.  
    6.     for(touch in iPhoneInput.touches){
    7.         if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
    8.             thing[touch.fingerId].transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 5));
    9.         }
    10.         if(touch.phase != iPhoneTouchPhase.Ended  touch.phase != iPhoneTouchPhase.Canceled) {
    11.             line[touch.fingerId] = touch.fingerId + " (" + touch.phase +") "+ " " + touch.position.ToString() + "";
    12.         } else {
    13.             line[touch.fingerId] = "";
    14.         }
    15.     }
    16.    
    17. }
    18.  
    19. function OnGUI () {
    20.     GUI.color = Color(0,0.9,0);
    21.     GUI.Label (Rect (20, 35, 500, 20), line[0]);
    22.     GUI.Label (Rect (20, 50, 500, 20), line[1]);
    23.     GUI.Label (Rect (20, 65, 500, 20), line[2]);
    24.     GUI.Label (Rect (20, 80, 500, 20), line[3]);
    25.     GUI.Label (Rect (20, 95, 500, 20), line[4]);   
    26. }
    27.  
    PS: If it's not too much trouble, can you try adding a Trail Renderer (Component > Particles > Trail Renderer) to these objects and tell me if it shows when you move them? Cause that's what's bugging me at the moment... Trail Renderers not working... :[
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks for this code!

    I'm having difficulties with it though.

    When I use this code, as described, all of the objects assigned in the "Thing" array go to the same finger touch location on the screen.

    Also, I am not getting the trail renders either on a new MBP using the low-power GPU ion iPhone graphics emulation mode.
     
  4. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Ditto. I am somehow failing to explain that I want a single finger touch on this item or location to do X, a single touch on a second item or location to do Y, etc. Whether these are buttons, locations, game objects etc. Each single touch potentially does a different thing.
     
  5. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Ditto. I am somehow failing to explain that I want a single finger touch on this item or location to do X, a single touch on a second item or location to do Y, etc. Whether these are buttons, locations, game objects etc. Each single touch potentially does a different thing, usually involving a common action like Application.LoadLevel etc.
     
  6. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    Well, the way that thing works is that if one finger touches the screen it gets finger ID 0 and keeps that until you let go. if you touch down 3 fingers they get ids 0, 1 and 2 and if you then let go with the first one that touched the screen, ID 0 becomes available again and if you then touch the screen again while still holding the other two, the new finger will get ID 0, since it has been freed up. My script doesn't try to recognize what you tap on and move that object, it simply assigns one object to each of the 5 touch-events that you can have going on simultaneously and puts it in the place where that event is currently happening. So if you only use one finger you'll always only move that same first object around.

    I'm not sure if that helps, if not please explain in more detail what it is you're trying to achieve! :]
    If you're trying to check if your touch hit an object, you might have to do some raycasting, I haven't tried anything like that yet myself...

    I'll attach the tiny little project that I have...
     

    Attached Files:

  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Oops!

    Sorry - it works just fine (except for the trail renderer).

    I had a rogue script in there that needed to be deleted.

    Great code! Thanks!
     
  8. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    So this is only then lacking the objects that I want to define in the array? IE, if I only have a single object I'm inerested in touching, then I could instead of an array just use var thing : GameObject; and define it in the inspector? Then change the transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 5)); to what I want to do. I will try.
     
  9. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Yeah, it works like a charm.

    So, is this pretty much the definitive way that we're going to process touch and multitouch in Unity iPhone?
     
  10. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    not sure if it's the ultimate way, but it's the one way I've figured out so far.
    feel free to improve it! :]
     
  11. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Much to my pleasant surprise, I found that Input.GetMouseButtonDown(0) and Input.mousePosition both work find on iPhone! I had a game already written and it just worked without changing any input code. Incredible! Needless to say, this makes testing on your computer a bit easier, even though the iPhone Remote is frickin' awesome!

    Can anyone else share their experiences about the extent to which the existing mouse-handling code works on iPhone?
     
  12. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
    How is that possible?
     
  13. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    We emulate the mouse with touches, so if you're just using e.g. OnMouseDown, that'll work just the same with touches.
     
  14. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Nicholas, do the emulated mouse routines also take into account touch events that occurred between frames like the iPhone input system does? Or do we need to abandon the mouse emulated routines if we need that functionality?
     
  15. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Fantastic. This make some stuff much much easier.
     
  16. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    And just to make sure folks are aware of it, we posted some example projects on the Resources area of our site:

    Unity iPhone Examples


    Hope that helps as well. :)
     
  17. robot-info

    robot-info

    Joined:
    Jul 19, 2011
    Posts:
    10
    Hi,

    i m beginner in unity,i want to convert this code from mouse input to iphone touch manipulation:



    var women:GameObject;

    var etat:int=1;


    function Start()
    {

    var fear=women.animation["fear"];
    fear.wrapMode=WrapMode.Loop;

    Debug.Log("script started");
    }


    function test()
    {
    yield WaitForSeconds(3);
    if(etat==0){
    etat=1;
    women.animation.Play();
    Start();

    Debug.Log("test");
    }

    }

    Debug.Log("anim mort");

    women.animation.Play("death");




    function OnMouseDown(){
    if(etat==1){
    Debug.Log("clique me");

    etat=0;
    women.animation.Play("death");
    test();
    }
    }

    can you help me please with an example well explained?

    thanks
     
  18. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Sure do a search really, I mean why not use search button? the forums are now full of examples of how to do touch. Why would yet more help anyone.
     
  19. robot-info

    robot-info

    Joined:
    Jul 19, 2011
    Posts:
    10
    I found forums for this but unfortunately there is no answer or the answer is not clear that 's why I introduced my problem. can you help me with a link to these forums you speak
     
  20. robot-info

    robot-info

    Joined:
    Jul 19, 2011
    Posts:
    10
    the problem is not to reach anywhere in the scene is why I used the function OnMouseDown what I want is when you touch a specific object on the stage action that will unfold as my scene has multiple objects and each has an animation
     
  21. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723