how to stop dragging when object is at the corner of screen?,how to stop dragging 2d object when it is going out of screen?

void OnMouseDown ()
{
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);

	offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

}

i am using this codeā€¦PleaseHelp :frowning:
void OnMouseDrag ()
{

	Vector3 point = Camera.main.ScreenToWorldPoint (Input.mousePosition);
	point.z = gameObject.transform.position.z;
	point.y = gameObject.transform.position.y;
	gameObject.transform.position = point;

}

,void OnMouseDown ()
{
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);

	offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

}

using this code please help :frowning:
void OnMouseDrag ()
{

	Vector3 point = Camera.main.ScreenToWorldPoint (Input.mousePosition);
	point.z = gameObject.transform.position.z;
	point.y = gameObject.transform.position.y;
	gameObject.transform.position = point;

}

Iā€™m not sure what your question is, but my best guess is that you donā€™t want your objects get dragged out of the screen? Youā€™re calculating offset but does not seem to use it.

You maybe able to use Viewport to check if the object is outside bounds of the viewport. Check this out.