Touched object snaps to center when dragged

I already found a very good script in the forum concerning dragging a 3D object on an xz-Plane!

private float maxPickingDistance = 10000;// increase if needed, depending on your scene size
	
	private Vector3 startPos;
	private Vector3 offset;
	
	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;
					//offset = pickedObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(startPos.x, startPos.y,pickedObject.transform.position.z));
					if (horPlane.Raycast(ray, out distance1))
					{
						pickedObject.transform.position = ray.GetPoint(distance1);
					}
				}
			}
			else if (touch.phase == TouchPhase.Ended)
			{
				pickedObject = null;
			}
		}
	}
}

I just can’t figure out how to fix the snapping to the center of the object when i start dragging. I found several Q/A in this forum where an offset is mentioned but when i try to implement any of those ideas in the code the debugger throws exceptions.
I would appreciate any ideas!

Thanks in advance!

milla

Alright - i tried again figuring out, how i can implement the offset. I almost got it. There is just one thing i could not get fixed. The setup allows dragging on the xz-plane. When I touch an object near the plane (y almost 0) and start dragging, it seams to work (object does not jump to my finger and is dragged very smooth).
BUT when i touch the object at the top (e.g. at the tip of a tower) the more i drag it, the more it moves away from my touch point. It moves in the same direction, but the distance between object and touch becomes bigger the longer i drag it.

Can anybody help me or give some advices? Thanks in advance!!!

milla

Here is the code:

using UnityEngine;
using System.Collections;
//code from http://answers.unity3d.com/questions/566327/drag-object-relative-to-camera.html

public class MyDragBehaviour2 : MonoBehaviour
{
	private float maxPickingDistance = 10000;// increase if needed, depending on your scene size
	
	private Vector3 offset;
	private float distance1;
	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))
				{
					horPlane.Raycast(ray, out distance1);
					pickedObject = hit.transform;
					offset = pickedObject.transform.position - ray.GetPoint(distance1); 
				}
				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)+offset;
						
					}
				}
			}
			else if (touch.phase == TouchPhase.Ended)
			{
				pickedObject = null;
			}
		}
	}
}