Raycast on touch

var hit: RaycastHit;
var manager : GameObject;

function FixedUpdate()
{
    if (Input.touchCount > 0) 
    { 
        var theTouch : Touch = Input.GetTouch(0);
		var ray = Camera.main.ScreenPointToRay(theTouch.position);
		if(Physics.Raycast(ray,hit,100))
        { 
			if(hit.transform.gameObject.tag == "planet")
			{
				if(Input.touchCount == 1)
	            {
					if(theTouch.phase == TouchPhase.Began)
					{
						var script : PlanetActionMenu;
						script = manager.GetComponent(PlanetActionMenu) as PlanetActionMenu;
						script.PlanetSelected();
						script.planet = hit.transform.gameObject;
						hit.transform.Translate(Vector3(1,0,0));
					}
				}
			}
        	if(hit.collider.gameObject.name == "CameraMoveCollider")
        	{
	 			if(Input.touchCount == 1)
	            {
		            if (theTouch.phase == TouchPhase.Moved) 
		            {	
		            	transform.Translate(theTouch.deltaPosition.x/-6,theTouch.deltaPosition.y/-6,0);
		            	if(transform.position.x < 5)
		            	{
		            		transform.position.x = 5;
		            	}
		            	if(transform.position.x > 485)
		            	{
		            		transform.position.x = 485;
		            	}
		            	if(transform.position.y < 5 )
		            	{
		            		transform.position.y = 5;
		            	}
		            	if(transform.position.y > 485)
		            	{
		            		transform.position.y = 485;
		            	}
		            }     
				}
			}
		}
	}
}

for some reason this never works:

			if(hit.transform.gameObject.tag == "planet")
			{
				if(Input.touchCount == 1)
	            {
					if(theTouch.phase == TouchPhase.Began)
					{
						var script : PlanetActionMenu;
						script = manager.GetComponent(PlanetActionMenu) as PlanetActionMenu;
						script.PlanetSelected();
						script.planet = hit.transform.gameObject;
						hit.transform.Translate(Vector3(1,0,0));
					}
				}
			}

i tried hit.collider.tag, hit.collider.gameObject.tag and hit.transform.tag. Nothing works, the 2nd if works just fine

Are you checking on editor? If you are checking on editor then this code would not work because on editor “Input.touchCount” will always be 0 and your first condition is “Input.touchCount > 0” that will remain false always on editor. So you have to write code so that work for both means on editor as well as on device.

try including the word ‘out’ before the word hit when firing your raycast. on line 10

  1. If you encounter such a problem, always use Debug.Log to try and figure out what’s wrong & use MonoDevelop to debug break and go step by step. This way you can see what the raycasting actually hit (if it hit anything at all)
  2. You might have forgotten to tag your object correctly, give it a boxcollider or anything of the sort.
  3. Assuming it hit your object and we got into the 2nd “if” - the fact that you’re doing this in FixedUpdate might be the source of the problem. A general rule of thumb is that you should only do physics related things in FixedUpdate. Input should be handled in Update.
  4. Another general rule of thumb is that if you want to get touch/mouse input, you should raycast at Mathf.Infinity - that way you will always “hit” something, even if it’s far away