Test if a touch event is located on a specific tag or layer

Hey all,
Kinda of new to Unity dev but I am long-time iOS developer and I have to say this world is awesome to work in and really make life easy when it comes to 3D.

My question pertains to how to use tags and layers properly. If this is really a simple question and it just requires me to read a tutorial again can you please point me in the right direction.

I am using a script on my main character which I found online
http://wiki.unity3d.com/index.php/Tap_to_Move_Drag_to_Look_iPhone

I would like to modify is so that instead of having the target being set to the ground layer it will only move to a specific target that specify in my landscape. I believe the code needs to be modified here somewhere just not sure what though.

void SetTarget(Vector2 screenPos)
	{
		Ray ray = _camera.ScreenPointToRay (new Vector3 (screenPos.x, screenPos.y));
		RaycastHit hit;
		int layerMask = 1 << 8; // Ground
		if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) 
		{
			targetPoint = hit.point;
			isMovingToTarget = true;
		}
	}

Thanks everyone for the help in advance and I look forward to learn and working with this community!

I’m not 100% what it is that you’re trying to achieve here, the ability to cast rays against multiple layers?

If so you can do one of two things:

  • Modify the current layermask to something like “int layerMask = 1<<8 | 1<<9 | 1<<10” to hit layers 8, 9 and 10
  • Make a public LayerMask variable, set the layers for it in the inspector and pass this mask into the ray cast. I use this method a lot just because it makes the whole process much more readable and easier for me to understand exactly which layers I’m ray casting against.

If this isn’t what you need just shout :slight_smile:

Check all examples shown in this page and educate yourself about layers and how to use them in your project