- Home /
Drag and drop picks an object too far away from mouse position
Working on a card game. "Canvas" hold all the objects. Its a World space canvas. It has the size of my screen.
"MazoFull" is a GameObject (a deck) and hold all the cards ("A001". "N001"...)
Im trying to drag the cards, but clicking on cards cant drag, as you can see in the video, dragging on that location, will drag a far away card. If I add a 3º card where I drag with the mouse pointer, occur the same, the rightmost card are dragged.
So, I think I know what is the problem, but dont know how to resolve it.
I think drag routine is hitting the canvas, but dont know how to "hide it".
video
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public GameObject itemBeingDragged;
Vector3 startPosition;
Transform startParent;
public void OnBeginDrag(PointerEventData eventData)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
startParent = transform.parent;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
}
}
DragHandler is attached to each card ("A001", "R001"....)
Answer by Luischo · Nov 19, 2018 at 01:37 PM
Looks like I didnt added camera to event camera in Canvas. That let me click in the right objects.
Answer by Luischo · Nov 19, 2018 at 01:23 PM
Added this class to each "card" and effectively doesnt show any time mouse is over a card. So, how can I hide that canvas?
void OnMouseOver()
{
//If your mouse hovers over the GameObject with the script attached, output this message
Debug.Log("Mouse is over GameObject.");
}
void OnMouseExit()
{
//The mouse is no longer hovering over the GameObject so output this message each frame
Debug.Log("Mouse is no longer on GameObject.");
}
Your answer
Follow this Question
Related Questions
Following instruction for drag and drop 3d object unity but not working 1 Answer
spawn and drag game object without leaving mouse button 1 Answer
NGUI Mouse position doesn't match with dragged object 0 Answers
Drag and Drop PlayerPrefs 2 Answers
Dragged Object, When it Collides with Another Object, Will Go Back to Initial Position 0 Answers