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

Can anyone, anyone at all, please provide a simple script example of how to use iPhoneTouch.position? Pretty please with sugar on it?

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! :]

var thing = new GameObject[5];
var line = new String[5];

function Update () {

  	for(touch in iPhoneInput.touches){
		if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
			thing[touch.fingerId].transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 5));
		}
		if(touch.phase != iPhoneTouchPhase.Ended  touch.phase != iPhoneTouchPhase.Canceled) { 
			line[touch.fingerId] = touch.fingerId + " (" + touch.phase +") "+ " " + touch.position.ToString() + ""; 
		} else {
			line[touch.fingerId] = "";
		}
	}
	
}

function OnGUI () {
	GUI.color = Color(0,0.9,0);
	GUI.Label (Rect (20, 35, 500, 20), line[0]);
	GUI.Label (Rect (20, 50, 500, 20), line[1]);
	GUI.Label (Rect (20, 65, 500, 20), line[2]);
	GUI.Label (Rect (20, 80, 500, 20), line[3]);
	GUI.Label (Rect (20, 95, 500, 20), line[4]);	
}

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… :[

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.

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.

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.

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…

99640–3861–$iphone_testarea_645.zip (489 KB)

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!

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.

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?

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! :]

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?

How is that possible?

We emulate the mouse with touches, so if you’re just using e.g. OnMouseDown, that’ll work just the same with touches.

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?

Fantastic. This make some stuff much much easier.

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. :slight_smile:

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

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.

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

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