W
W
WasTabon2021-03-28 14:57:52
Unity
WasTabon, 2021-03-28 14:57:52

How to make drag and drop non UI object?

I tried IPointerUpHandler (or something like that), but only UI objects are affected there + there is no interaction with physics

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WasTabon, 2021-03-29
@WasTabon

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DND : MonoBehaviour, IBeginDragHandler, IDragHandler
{
    public Transform transform;
    public Vector2 vector2;
    private void Awake()
    {
        transform = GetComponent<Transform>();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("OnDrag");
        vector2 = Camera.main.ScreenToWorldPoint(eventData.position);
        transform.position = new Vector3(vector2.x, vector2.y, 0);
        //transform.position = eventData.pointerCurrentRaycast.screenPosition;
    }
}

+ Add an EventSystem object and everything will work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question