Drag object up/down and automatically move it along Z axis/towards another object at same time


Say I have 2 objects (Object and Slot) with Object close to the camera and Slot further away (along the z-direction). I would like to be able to drag Object along the X and Y axes and have it automatically move towards Slot (along Z) depending on how far I drag it. I have calculated the offset and the direction between Object and Slot but am not too sure how to implement this kind of movement. For my scene, the Slot is higher up than the Object (has a larger Y-value). I have attached a screenshot to show what I mean. Basically, dragging the Object from the starting point in image up along the Y (by Y offset) should also move it in the Z, by Z offset, therefore placing at the same position as the Slot.

Any help would be great, thanks.

using UnityEngine;
using System.Collections;

public class DoMath : MonoBehaviour {

	public GameObject obj;
	public GameObject slot;
	public float speed;

	void Update () {
		Vector3 direction = obj.transform.position - slot.transform.position;
		float xDiff = GetDiff (obj.transform.position.x, slot.transform.position.x);
		float zDiff = GetDiff (obj.transform.position.z, slot.transform.position.z);
		float distance = Mathf.Sqrt((xDiff * xDiff) + (zDiff * zDiff));

		obj.transform.position = Vector3.Lerp (obj.transform.position,
			obj.transform.position + direction * distance,
			Time.deltaTime * speed);

	float GetDiff(float a, float b){
		if(a > b){
			return a - b;
		if (b > a) {
			return b - a;
		} else {
			return 0f;

Didn’t try this before posting it. Will probably get the job done but be wacky. Hope it helps.