Hello there. So I’m fairly new to Unity, and have been following tutorials and the such. I’m now in the process of making my first game. Since I’m new, I’ve been adapting things from tutorials and example projects to build my game.
I’ve decided to reproduce a classic card game. For this card game, I’d like to drag cards, which would follow the mouse pointer around, until dropped in certain zones. I’d like to use the IDragHandler to make a script which I can attach to UI elements on a canvas to have their position adjusted to the position of the mouse when it is being dragged.
To do this, I need to detect when an object is dragged. Baby steps first, using the following code I should get a console log when I begin and stop dragging objects :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
void Start()
{
Debug.Log("GameStart");
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
}
public void OnDrag(PointerEventData eventData)
{
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
}
}
I attached this script to a UI image, parented to a panel, parented to a Canvas with an EventSystem in the scene. I also added a Canvas Group component to the image and have interactable and blocks raycasting enabled. The Camera also has a 2-d Physics Raycaster attached. (I’m sure some of this is unnecessary, but I tried them anyhow)
When I run the scene and attempt to drag and drop my image, I expect the console to have three logs - GameStart, OnBeginDrag and OnEndDrag. However, all I have is GameStart.
I’ve compared to the Unity Samples : UI store asset and I also checked out a tutorial video by quill18 creates at this youtube link. I should be getting console debug logs, but I’m not and am at a loss as to what to check for next.
Any help would be greatly appreciated.
I still have the problem... :/ the project worked fine on Unity 5 but since i updated to 2019 i can't OnDrag my Joystick on any mobile.. only works in editor. Help?
– KOORYPerhaps you should start your own question? What you're asking isn't clear. There's no mention of a joystick anywhere in this question
– Hunted223