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 Drops Don't Work. Please help.

Discussion in '2D' started by d083174, Nov 7, 2020.

  1. d083174

    d083174

    Joined:
    Oct 8, 2020
    Posts:
    11
    It seems that no matter which drag and drop script I use, whether it's from a tutorial on youtube or from an online class, none of them work on my laptop when I attach the script to an image. I've tried at least eight different scripts, each one working perfectly in the videos but never for me. Some scripts have handlers and some don't. It's almost as if nothing responds to my cursor.

    I've experimented on two versions of Unity on my laptop thinking maybe the program was corrupt, and yet the problem persists in both versions. Why is this happening? Does anyone have this same issue? If so, what's going on? Do I need to configure something?

    Any help would be appreciated.
     
  2. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    Are you dragging a game object or canvas element?
    Can you show one of the scripts you have tried?
     
  3. d083174

    d083174

    Joined:
    Oct 8, 2020
    Posts:
    11
    I'm trying to drag a canvas element. It's a simple image UI like in the tutorials. I've tried eight different scripts but here are two:

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

    public class DragandDrop : MonoBehaviour
    {
    private bool isDragging;
    public void OnMouseDown()
    {
    isDragging = true;
    }
    public void OnMouseUp()
    {
    isDragging = false;
    }

    // Update is called once per frame
    void Update()
    {
    if (isDragging)
    {
    Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    transform.Translate(mousePosition);
    }
    }
    }


    and a more complicated one:

    using UnityEngine;
    using System.Collections;

    public class DraggableTest : MonoBehaviour
    {
    public bool UsePointerDisplacement = true;
    // PRIVATE FIELDS
    // a flag to know if we are currently dragging this GameObject
    private bool dragging = false;
    // distance from the center of this Game Object to the point where we clicked to start dragging
    private Vector3 pointerDisplacement = Vector3.zero;
    // distance from camera to mouse on Z axis
    private float zDisplacement;
    // MONOBEHAVIOUR METHODS
    void OnMouseDown()
    {
    dragging = true;
    zDisplacement = -Camera.main.transform.position.z + transform.position.z;
    if (UsePointerDisplacement)
    pointerDisplacement = -transform.position + MouseInWorldCoords();
    else
    pointerDisplacement = Vector3.zero;
    }
    // Update is called once per frame
    void Update ()
    {
    if (dragging)
    {
    Vector3 mousePos = MouseInWorldCoords();
    //Debug.Log(mousePos);
    transform.position = new Vector3(mousePos.x - pointerDisplacement.x, mousePos.y - pointerDisplacement.y, transform.position.z);
    }
    }
    void OnMouseUp()
    {
    if (dragging)
    {
    dragging = false;
    }
    }
    // returns mouse position in World coordinates for our GameObject to follow.
    private Vector3 MouseInWorldCoords()
    {
    var screenMousePos = Input.mousePosition;
    //Debug.Log(screenMousePos);
    screenMousePos.z = zDisplacement;
    return Camera.main.ScreenToWorldPoint(screenMousePos);
    }
    }

    I have some that uses the IDragHandler, IBeginDragHandler and IEndDragHandler, but these don't work either.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @d083174

    Use code tags... edit your post, otherwise it is hard to read and looks nasty.

    Also, there is a separate forum for uGUI / UI questions. This forum is for 2D features.