Clicking and Dragging 2D object random movement?!
Hi all,
Very new to Unity and game creation in general but very excited about creating some cool little games of my own. I'm currently in the very early stages of creating a simple 2D trading card game. At this stage, I have:
A canvas which has a background image for my simple game
Two panels in the canvas, one acting as a player's hand, the other as a drop zone for a card. These have horizontal layout groups to ensure that a child card looks right.
a "Card" prefab, consisting of an image (card picture and background) and two other child images
This prefab also contains a simple script which SHOULD allow me to simply click the card, hold my mouse button down and drag the card with my mouse. Once the click ends, the card returns to its original position.
MY PROBLEM IS - that when I click and begin to drag, the card instantly changes its position off to the right, and upwards, depending on the position of the parent panel relative to the canvas.
I have absolutely no idea what is causing this, and have tried everything I can think of, including googling the problem. I've included the script below, however I'm positive that this isn't the problem. I'm hoping this is some simple thing that I'm missing and would GREATLY appreciate anyone's help. Thanks very much.
using UnityEngine; using System.Collections; using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public static GameObject itemBeingDragged;
Vector3 startPosition;
#region IBeginDragHandler implementation
public void OnBeginDrag (PointerEventData eventData)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
}
#endregion
#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag (PointerEventData eventData)
{
itemBeingDragged = null;
transform.position = startPosition;
}
#endregion
}
Your answer
Follow this Question
Related Questions
Pipe Game Water Flow 0 Answers
KillPlayer, Null Reference Exception. 2 Answers
Player won't stop moving when key is released 1 Answer
How to check if player remains on a path? (2D) 1 Answer
No overload for method 'fireBullet' takes 0 arguments 1 Answer