Basic touch code for C#

EDIT :
I tired using the touch manager code thats now opensource, I’m having a very hard time with it …

Hi all, I need some basic touch code for C#

Here’s the JS version that I already have

function Update () {
//if ( Got == true ) {
//SeqPlay();

//}

 //KeyD(); 
	for (var touch : Touch in Input.touches) {
		if (touch.phase == TouchPhase.Began) {
			// Construct a ray from the current touch coordinates
			var ray = Camera.main.ScreenPointToRay (touch.position);
			var hit:RaycastHit = new RaycastHit(); 
			if (Physics.Raycast (ray,hit,1000)) {
				
           	if(hit.collider.gameObject == this.gameObject) {
           	// code to run 
//AllofIt();
       }
}
}
}
}

And here’s the C# code that i’ve attempted to write…

	void Update () {

	 foreach (Touch touch in Input.touches) {
		if (touch.phase == TouchPhase.Began) {
			// Construct a ray from the current touch coordinates
		Ray 	 ray = Camera.main.ScreenPointToRay (touch.position);
			  RaycastHit hit = new RaycastHit(); 
			if (Physics.Raycast (ray,hit,1000)) {
				
           	if(hit.collider.gameObject == this.gameObject) {
          
			// the code we need to run 
						// Add();
						//	AllofIt();
       }
}
}

You shouldn’t do raycast for each object, this is horribly ineffective. Each objects that has similar code in it will do multiple casts.

It’s better to make a single touchmanager that does the raycasts once and notify all the objects it hits. More effective and less repetitive code

Ok, but how do I get the raycast working in C#, my new project is totally C#, and I really don’t want to use JS just for touch management

I only have about 3 objects that need this , but I’m going to look in the unity wiki for said touch manager code- fresh out of cash for asset store stuff

I received a tweet yesterday which mentions interactivelab open sourced their touch framework on github :

https://github.com/interactivelab/touchscript

I cloned the project but have not had a look at it yet.

thought I should just mention in your raycast c# code example :

The hit object does not need to be initiated and an out statement is then used in the Raycast method attributes:

RaycastHit hit;

if (Physics.Raycast (ray, out hit, 100)) {
//some code here
}

It looks quite nice so far, though it’s a bit confusing that he is using DPI, but doesn’t seem to have any code to retrieve the actual DPI (in the Manager class it’s hardcoded and in the options it switches between the set value and editor DPI). May be an issue, since on Android the DPI can vary anything from 100dpi to 300+ dpi and 326 dpi or so on iPhone’s Retina.

But other than that it seems well design. Should test it too later when I get more time. Thanks for the find

I"m trying to use this , at the moment, and its very , umm difficult .

Like so far I can’t even recreate the sample scene .

I guess if I’m ever able to make use of it , it’ll be awesome .

bumping this since I still need help