A
A
Alexander Sinitsyn2021-02-07 11:51:10
Unity
Alexander Sinitsyn, 2021-02-07 11:51:10

EventSystem, what should I write in OnDrag for dragging?

using UnityEngine;
using UnityEngine.EventSystems;

public class DragDrop : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData eventData) {
        // Вот тут...
    }
}


One article writes
transform.position = eventData.position;
// at the same time, the object is blown to coordinates in thousands

In another:
transform.position = eventData.pointerCurrentRaycast.screenPosition;
// while the object jerks between coordinates in thousands and zero

In the third:
transform.localPosition += new Vector3(eventData.delta.x, eventData.delta.y, 0) / transform.lossyScale.x;
// The object is only dragged far away from the cursor, but it is at least visible )

By experimenting with the third one, I came up with the correct option:
transform.localPosition += new Vector3(eventData.delta.x, eventData.delta.y, 0);

Can someone write how to do the drag and drop correctly and at what settings does the first, second, third work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-02-07
@a_u_sinitsin

The correct option is the one that suits your conditions and system.
So all these options work. But they depend on the canvas settings, on what exactly you are pulling, on the object hierarchy (and in the case of yuaya, also on the parent settings)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question