Problem of drag a 3d object by touch

I have a 3d model and want to drag it by touch.
I have used the code :
using UnityEngine;
using System.Collections;

public class DragObject : MonoBehaviour
{
private float maxPickingDistance = 10000;// increase if needed, depending on your scene size

private Vector3 startPos;

private Transform pickedObject = null;

// Update is called once per frame
void Update () 
{
	foreach (Touch touch in Input.touches) 
	{
		//Create horizontal plane
		Plane horPlane = new Plane(Vector3.up, Vector3.zero);
		
		//Gets the ray at position where the screen is touched
		Ray ray = Camera.main.ScreenPointToRay(touch.position);
		
		if (touch.phase == TouchPhase.Began) 
		{
			RaycastHit hit = new RaycastHit();
			if (Physics.Raycast(ray, out hit, maxPickingDistance)) 
			{ 
				pickedObject = hit.transform;
				startPos = touch.position;
			} 
			else
			{
				pickedObject = null;
			}
		} 
		else if (touch.phase == TouchPhase.Moved) 
		{
			if (pickedObject != null) 
			{
				float distance1 = 0f;
				if (horPlane.Raycast(ray, out distance1))
				{
					pickedObject.transform.position = ray.GetPoint(distance1);
				}
			}
		} 
		else if (touch.phase == TouchPhase.Ended) 
		{
			pickedObject = null;
		}
	}
}

}

I can drag the object but the problem is I can only drag different part of it, not the whole object. I have grouped them to a empty game object but still cant drag the whole object
How can solve it?

Do all objects in the hierarchy have separate colliders?

Also, how deep does the hierarchy go, is there only one level?

The general issue here is that you are selecting a specific object hit by raycast. If every object in the hierarchy has a collider then the raycast will hit only one of those. To solve this you can do one of two things:

  • Select the parent of the object hit, instead of the object itself. Note that this will only work if your hierarchy has only one level. The code is this

    pickedObject = hit.transform.parent;

  • Delete the colliders on the children and create one single collider on the parent gameobject instead. This will guarantee that the raycast hits the parent.