Swipe to move object: object sometimes flying off screen even though value is clamped

Hello,

I’m quite stuck on how to solve this problem:

When the player isn’t moving, and the camera is in orthographic mode - I want the player to be able to swipe up the screen and move an object up to the top of the screen, and when it reaches the top of the screen - to fall back down to the bottom of the screen.

The problem I’m having is that occasionally when you swipe (I think when you swipe particularly fast - though it’s hard to tell what triggers this) the bag flies off screen.
This doesn’t happen in Unity remote - but only when the game is built: though I think this is probably because the touch values seem to be calculated differently for unity remote and an android build.

It seems that the values aren’t being clamped correctly - as I’m not sure why the bag disappears off screen.
Does anyone know how I’ve set this up wrong?

If anyone has any pointers, that would be really great!

Best,
Laurien

EDIT: updated script…

using UnityEngine;
using System.Collections;

public class TouchDetect : MonoBehaviour {
	
	float speed = 0.5f;
	public float radius = 3.0f;
	public PerspectiveSwitchFinal persp;
	Vector3 centrePt = new Vector3(0,0,0);
	public bool moveToCentre = false;
	public GameObject bag;


	public AccelerometerMagLowPass accel;

	public Vector3 bagCurrentPos;
	public float bagCurrentPosFloat;


	void Update() 
	{
		if (persp.orthoOn == true)
	{

			centrePt.x = transform.position.x;
			centrePt.y = transform.position.y;
			// z never changes from zero

			if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) 
		{
			if (Input.GetTouch(0).deltaPosition.y >= 0.5)
			{
				speed = 0.5f;
				float dragValue = (Input.GetTouch(0).position.magnitude / Input.GetTouch(0).deltaTime / 200);
			
				Vector3 movement = new Vector3(0,0, dragValue); 
				Vector3 newPos = transform.position  + movement * Time.deltaTime * speed; 
				Vector3 offset = newPos - centrePt; 
	
				//transform.position = centrePt + Vector3.ClampMagnitude(offset, radius);
				transform.position = new Vector3 (Mathf.Clamp(offset.x, -2f, 2f), Mathf.Clamp(offset.y, 0f, 7f), Mathf.Clamp(offset.z, 0f, 7f));
			}

			else if (Input.GetTouch(0).deltaPosition.y <= -0.5) 
			{
				speed = 0f;
				moveToCentre = true;
			}

			else if ((Input.GetTouch(0).deltaPosition.y > -0.5) &&  (Input.GetTouch(0).deltaPosition.y < 0.5))
			{
				speed = 0f;
				moveToCentre = true;
				
			}
		}
		
		
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) 
		{

			bagCurrentPosFloat = this.transform.position.z;
			
			moveToCentre = true;
		}

		if (moveToCentre)
		{
			transform.position = Vector3.MoveTowards(transform.position, centrePt, Time.deltaTime * 10f);
		
			

	
			if (Vector3.Distance(transform.position, centrePt) <= 0.1)
			{
			moveToCentre = false;
			transform.position = centrePt;
			}
		}

The problem is that every frame you set a new central point, thus not restricting its position since the anchor is constantly moving.

Line25: centrePt.x = transform.position.x;
Line26: centrePt.y = transform.position.y;