// Hi, my code is working fine but I want put a limit on the edges of the screen so that the dragged object don’t go outside of the screen. How do I do that? Here’s my code.
void OnMouseDown()
{
if (IsDragable == true)
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0, screenPoint.z));
}
}
void OnMouseDrag()
{
if (IsDragable == true)
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, 0, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}