title: My draggable UI elements are also dragging camera? How do I prevent UI dragging from moving the camera?
body: <p>So I have UI elements that can be dragged, so that a prefab can be instantiated <code>OnDragEnd()</code>.  I am using below code attached to the UI buttons to achieve this</p>
  <pre><code>public class MyButton : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
  {
      ..
      ..
      ..
  
  
      public void OnDrag(PointerEventData eventData)
      {
          dragRect.anchoredPosition += eventData.delta;
      }
  
      public void OnBeginDrag(PointerEventData eventData)
      {
          // if we're on deploy cooldown, dont allow dragging this button
          if (disableDrag )
          {
              eventData.pointerDrag = null;
          }
      }
  
      public void OnEndDrag(PointerEventData eventData)
      {
          RaycastHit hit;
          Ray ray = mainCamera.ScreenPointToRay(eventData.pointerCurrentRaycast.screenPosition);
          Physics.Raycast(ray, out hit);
          if (hit.collider.gameObject.tag == &quot;Ground&quot;)
          {
              createUnit(hit);
              disableDrag = true;
          }
      }
  
  }
  </code></pre>
  <p>This works fine without any problems, but recently I also implemented the ability to drag the camera. I attached the following code to my camera to achieve this:</p>
  <pre><code>public class CameraController : MonoBehaviour
  {
      public float movementTime;
      public Vector3 newPosition;
      private Vector3 startPosition;
      public Vector3 dragStartPosition;
      public Vector3 dragCurrentPosition;
      public float xLimit1, xLimit2;
      float clampedX;
      Camera mainCamera;
  
      private void Start()
      {
          mainCamera = Camera.main;
          newPosition = transform.position;
          startPosition = transform.position;
      }
  
      // Update is called once per frame
      void Update()
      {
  
          HandleMouseInput();
          clampedX = Mathf.Clamp(newPosition.x, xLimit1, xLimit2);
          transform.position = Vector3.Lerp(transform.position, new Vector3 (clampedX, startPosition.y, startPosition.z), Time.deltaTime * movementTime);
      }
  
      void HandleMouseInput()
      {
          // when mouse is pressed,
          if (Input.GetMouseButtonDown(0))
          {
              if (!EventSystem.current.IsPointerOverGameObject())
              {
                  Plane plane = new Plane(Vector3.up, Vector3.zero);
                  Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
                  float entry;
  
                  if (plane.Raycast(ray, out entry))
                  {
                      dragStartPosition = ray.GetPoint(entry);
                  }
              }
          }
          // if it is still held down
          if (Input.GetMouseButton(0))
          {
              if (!EventSystem.current.IsPointerOverGameObject())
              {
                  Plane plane = new Plane(Vector3.up, Vector3.zero);
                  Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
                  float entry;
  
                  if (plane.Raycast(ray, out entry))
                  {
                      dragCurrentPosition = ray.GetPoint(entry);
  
                      newPosition = transform.position + dragStartPosition - dragCurrentPosition;
                  }
              }
          }
  
      }
  }
  </code></pre>
  <p>And the camera script works as well as it behaves as expected and the camera drags with mouse. But the problem is that when I drag my UI button onto screen that moves the camera also.</p>
  <p>I tried to include the if condition of checking if its over a UI <code>if (!EventSystem.current.IsPointerOverGameObject())</code> But this does not seem to work, the camera still drags when I drag the UI button.</p>
  <p>How do I make them work together?</p>