Q:

How can I restrict gameobject from moving beyond line in unity

I have this red apple that needs to move only inside the green apple line. If it tries to move beyond line it will be stopped at the green line and can' move beyond. How can I do that?

This is the code I use to move a red apple.
 Camera mainCamera;
 float zAxis = 0;
 Vector3 clickOffset = Vector3.zero;

 // Use this for initialization
 void Start()
 {

     mainCamera = Camera.main;
     mainCamera.gameObject.AddComponent<Physics2DRaycaster>();

     zAxis = transform.position.z;
 }

 public void OnBeginDrag(PointerEventData eventData)
 {
     clickOffset = transform.position - mainCamera.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, zAxis));
 }

 public void OnDrag(PointerEventData eventData)
 {
     //Use Offset To Prevent Sprite from Jumping to where the finger is
     Vector3 tempVec = mainCamera.ScreenToWorldPoint(eventData.position) + clickOffset;
     tempVec.z = zAxis; //Make sure that the z zxis never change
     /*
     Debug.Log(tempVec.x);
     if (tempVec.x < 90)
     {
         tempVec.x = 90;

     }
     if (tempVec.x > 310)
     {
         tempVec.x = 310;

     }
     */
     transform.position = tempVec;
 }

 public void OnEndDrag(PointerEventData eventData)
 {

 }

A:

Don't use code for this use colliders on both sides.

