- Home /
Click and Drag - How to?
I've been running around trying to figure this out for almost a week now, but none of the Q&As I've seen have provided me with a functional answer. I'm pretty new to Unity overall (just picked it up last week) and between school and my job I've only been able to devote a little of my free time to watching tutorials and learning that way.
All I want to do is a simple 2D click-and-drag, and for the program to respond when the object is dragged to a spot. If it is not in the right spot when the mouse button is released, then it will snap back to its original position and play a failure sound, but if it is brought into the right bounding box/circle, it will play a success sound and snap it into the final position.
This doesn't seem like it should be that difficult for a 2D game, but I don't fully understand the way that Unity sprites and scripts interact yet. I have made the draggable-object a rigidbody and tried to apply the DragRigidbody script to it, but that's not working either. Could I get some help please?
This is a multi-part problem and should be broken down in to smaller chunks and handled independently.
You also need to specify if you're using the new UI system or not, I have written a couple of UI drag scripts recently. Not exactly what you're asking for but if using the new UI would give you a pretty good start, but not relevant for all instances.
$$anonymous$$mmpies - I guess? I only downloaded Unity3D last week
meat5000 - I know it does, and right now I just want to figure out how to drag the object. $$anonymous$$y problem is that I don't know enough about the Unity interface or functionality, and I haven't really gotten much farther through the tutorials than "These are the buttons, this is how you place a thing on your screen"
Answer by Mmmpies · Feb 01, 2015 at 10:04 PM
OK @JonMcDevitt then this will hopefully get you started.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class DragUI : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
private bool mouseDown = false;
private Vector3 startMousePos;
private Vector3 startPos;
private bool restrictX;
private bool restrictY;
private float fakeX;
private float fakeY;
private float myWidth;
private float myHeight;
public RectTransform ParentRT;
public RectTransform MyRect;
void Start()
{
myWidth = (MyRect.rect.width + 5) / 2;
myHeight = (MyRect.rect.height + 5) / 2;
}
public void OnPointerDown(PointerEventData ped)
{
mouseDown = true;
startPos = transform.position;
startMousePos = Input.mousePosition;
}
public void OnPointerUp(PointerEventData ped)
{
mouseDown = false;
}
void Update ()
{
if (mouseDown) {
Vector3 currentPos = Input.mousePosition;
Vector3 diff = currentPos - startMousePos;
Vector3 pos = startPos + diff;
transform.position = pos;
if(transform.localPosition.x < 0 - ((ParentRT.rect.width / 2) - myWidth) || transform.localPosition.x > ((ParentRT.rect.width / 2) - myWidth))
restrictX = true;
else
restrictX = false;
if(transform.localPosition.y < 0 - ((ParentRT.rect.height / 2) - myHeight) || transform.localPosition.y > ((ParentRT.rect.height / 2) - myHeight))
restrictY = true;
else
restrictY = false;
if(restrictX)
{
if(transform.localPosition.x < 0)
fakeX = 0 - (ParentRT.rect.width / 2) + myWidth;
else
fakeX = (ParentRT.rect.width / 2) - myWidth;
Vector3 xpos = new Vector3 (fakeX, transform.localPosition.y, 0.0f);
transform.localPosition = xpos;
}
if(restrictY)
{
if(transform.localPosition.y < 0)
fakeY = 0 - (ParentRT.rect.height / 2) + myHeight;
else
fakeY = (ParentRT.rect.height / 2) - myHeight;
Vector3 ypos = new Vector3 (transform.localPosition.x, fakeY, 0.0f);
transform.localPosition = ypos;
}
}
}
}
It uses the new UI so it only works for the new UI and needs ScrollRects, I know you're new but I don't know how you intend your game to be setup.
The thing is Unity, in fact any programming, is so varied in the way it can be implemented that there are no correct answers until you get down to real specifics @meat5000 is no fool get on YouTube and start some tutorials. Hey it doesn't cost anything, I'd recommend C# but I know @meat5000 is JS/Boo kinda person so I'm not gonna argue with someone with that level of Unity karma points.
the choice is yours but DO use YouTube before answers, it's what I did (by accident, I didn't spot Answers) and it saved me a lot of pointless asking questions.
Answer by meat5000 · Apr 23, 2016 at 12:43 PM
Check out the DragPanel tutorial
https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows
It explains the use of IDragHandler and OnDrag callback for drag functionality on the UI.