I am raycasting to detect collision with objects that are on the controllable layer. For some reason objects that are not on the controllable layer are also getting hit. Here is the code where I call the raycast.
if (Input.touches.Length > 0)
{
RaycastHit hit;
if (Physics.Raycast(firstPersonCamera.ScreenPointToRay(Input.GetTouch(0).position), out hit, controllableLayer))
{
Debug.Log("Object hit");
Debug.Log(hit.transform.gameObject.name);
Touch touch = Input.GetTouch(0);
GameObject objectHit = hit.transform.gameObject;
if (dragging)
{
Vector3 cursorScreenPoint = new Vector3(touch.position.x, touch.position.y, screenPoint.z);
Vector3 cursorPosition = firstPersonCamera.ScreenToWorldPoint(cursorScreenPoint) + offset;
objectHit.transform.position = cursorPosition;
}
// Handle finger movements based on touch phase.
switch (touch.phase)
{
// Record initial touch position.
case TouchPhase.Began:
screenPoint = firstPersonCamera.WorldToScreenPoint(objectHit.transform.position);
offset = objectHit.transform.position - firstPersonCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, screenPoint.z));
dragging = true;
break;
// Determine direction by comparing the current touch position with the initial one.
case TouchPhase.Moved:
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
dragging = false;
break;
}
}
else
{
Debug.Log("Nothing hit");
}
}
Also here is where I declare the layermask
void Awake()
{
firstPersonCamera = GameObject.Find("First Person Camera").GetComponent<Camera>();
controllableLayer = LayerMask.NameToLayer("Controllable");
Debug.Log(LayerMask.LayerToName(controllableLayer));
}