constrain 2d gameobject to finger position (js)

hello,i am new to unity and i have been relentlessly trying to accomplish this with no success. what i would like to do is this - have an empty gameobject in the scene which is not active, but when the user starts the swipe the gameobject becomes active and automatically snaps to the position of the finger on the screen, and is then constrained to the position of the finger on the screen and follows the swipe regardless of the speed of the swipe or direction. what i have tried so far is this -
instantiate gameobject when the touchphase.moved is started, destroy the gameobject on touchphase.ended, and attach the following script to the prefab:

var speed : float = 0.1;


function Update () {
		if (Input.touchCount > 0 && 
		  Input.GetTouch(0).phase == TouchPhase.Moved) {
		
			
			var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
			transform.Translate (touchDeltaPosition.x * speed, 
						touchDeltaPosition.y * speed, 0);
		}
	}

but this did not work, what happened is that prefabs were continuously instantiated as long as the touchphase.moved was happening,i only wanted one spawned per swipe motion. additionally the prefab was being spawned at 0,0,0, and not at the touch point (probably a problem with the spawn script).

so then i thought that i could have the gameobject in the scene, and then automatically snap/start at/ move to the position of the finger when the swipe begins. but i am not sure how to get this done. i am using a trail renderer to see where the gameobject is moved during the swipe. i am also using JavaScript. any help with this will certainly be greatly appreciated, i have been trying a variety of different methods, but nothing seems to work, also if someone could point out a better method with scripting i would love to learn from it. thank you in advance.

You need to use the Touch.position, not the Touch.deltaPosition, and that position must be converted from a screen position to a world position. Here is a bit of example code (using the mouse). To test:

  • Start a new scene in the editor
  • Attach the following script to an empty game object
  • Create a prefab
  • Select the empty game object with the script, and drag the prefab to the ‘prefab’ variable on the script.
  • The ‘dist’ parameter is the distance in front of the camera to place objects. You may want to set this value.

#pragma strict

var prefab : Transform;
var dist : float = 10.0;
var curr : Transform;

function Update() {
	if (Input.GetMouseButtonDown(0)) {
		var pos = Input.mousePosition;
		pos.z = dist;
		pos = Camera.main.ScreenToWorldPoint(pos);
		curr = Instantiate(prefab, pos, Quaternion.identity);
	}
	if (Input.GetMouseButton(0)) {
		pos = Input.mousePosition;
		pos.z = dist;
		curr.position = Camera.main.ScreenToWorldPoint(pos);
	}
}

You’ll have to convert the code to touch, but the core of the change is just replacing ‘Input.mousePosition’ with the Touch.position of the touch you are using.