Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Drag and Drop, how do i get the offset to work so that the object does not snap to my cursor?

Discussion in 'Scripting' started by Etzix, Mar 17, 2020.

  1. Etzix

    Etzix

    Joined:
    Apr 8, 2014
    Posts:
    2
    Hi!
    I've looked through everywhere online and seen this question answered countless times, but when i try to implement it myself it just never works.

    Basically i want to drag an item, but when i pick it up i don't want it to "Snap" to my cursor.

    I know i have to get the offset from where i clicked on the object and where the center of the object is but when trying to implement it, it doesnt work.

    Heres my working code (Using IDragHandler):

    Code (CSharp):
    1.     public void OnDrag(PointerEventData pointerEventData)
    2.     {
    3.         if (editMode) {
    4.             mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
    5.             transform.position = mousePosition;
    6.         }
    7.     }
    I tried this and countless similar things, and i feel like they should work but obviously i'm missing something.

    Code (CSharp):
    1.     public void OnPointerDown(PointerEventData eventData)
    2.     {
    3.         clickOffset = transform.position - mainCamera.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, zAxis));
    4.     }
    5.  
    6.     public void OnDrag(PointerEventData eventData)
    7.     {
    8.         //Use Offset To Prevent Sprite from Jumping to where the finger is
    9.         Vector3 tempVec = mainCamera.ScreenToWorldPoint(eventData.position) + clickOffset;
    10.         tempVec.z = zAxis; //Make sure that the z zxis never change
    11.  
    12.         transform.position = tempVec;
    13.     }
    I'm using the Perspective Camera, but i don't feel like that should make a difference?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,719
    One easy way is to create a temporary GameObject at the mouse pointer when the click starts, and parent the thing you want to move to that.

    When you finish moving it and the click releases, unparent it and destroy the temporary GameObject.

    Another way is to compute the offset and add it in as you drive the position of the dragged object.
     
  3. Etzix

    Etzix

    Joined:
    Apr 8, 2014
    Posts:
    2
    Hey! Thanks for your reply.

    I would prefer to calculate the offset, but as i tried to explain in my OP, i'm having issues with the calculation causing my object to snap towards the wrong position on screen.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,719
    Use
    Debug.Log()
    to ensure the
    OnPointerDown
    offset calculator is actually getting called before the
    OnDrag
    in all cases. If it isn't, you might need a boolean lockout to ignore OnDrag callss until OnPointerDown gets hit and your offset is valid.

    Also, technically you are giving a different Vector3 argument to
    mainCamera.ScreenToWorldPoint
    in those two call sites. Unify that first, to ensure it's not the z component difference. If you have a perspective camera, this would make a difference.
     
  5. mhmad2212

    mhmad2212

    Joined:
    Sep 12, 2019
    Posts:
    5
    I also have this problem ... did you find a solution?