- Home /
how i can drag a sprite in 2D mode??
I have setup 4.6 in 2D mode and I'm trying to simply drag a sprite on click I saw all the resources and I did not get to solve my problem, I have this code only works in 3D but i need it in 2D mode using UnityEngine; using System.Collections;
public class TouchControl: MonoBehaviour {
private float dist;
private Vector3 v3Offset;
private Plane plane;
//public player transform;
void OnMouseDown() {
plane.SetNormalAndPosition(Camera.main.transform.forward, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float dist;
plane.Raycast (ray, out dist);
v3Offset = transform.position - ray.GetPoint (dist);
}
void OnMouseDrag() {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float dist;
plane.Raycast (ray, out dist);
Vector3 v3Pos = ray.GetPoint (dist);
transform.position = v3Pos + v3Offset;
you have mentioned 4.6 ui, so are you planning to drag a gameobject (2d sprite) or an UI image (which is an UI)?
you can use OnDrag() id it is UI, not sure if it works on 2d gameobject (should work though)
Answer by Mmmpies · Jan 31, 2015 at 05:39 PM
Answered this for someone the other day, this should do it:
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;
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;
}
}
}
Just put that on the UI element you want to drag.
EDIT
I'll be amazed if there isn't a better way within the new UI to restrict movement but I couldn't find it easily, tried limiting a scrollRect but had to code it to get it working. Anyway this should get you going, just drag the parent panel onto the ParentRT and the image onto MyRect.
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 / 2;
myHeight = MyRect.rect.height / 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;
}
}
}
}
and if that answers your question tick it as answered, just stops others from spending time trying to fix it, and lets people searching know this a way doing what you asked, but hope it works for you.
is that it is possible to make the movement of this ui image limited, is to say that I can move right in the surface of the canvas that this object belongs?? how i can do this
Not something I've tried, give me a while and I'll look into it.
Your answer
Follow this Question
Related Questions
[2D Mode] Trying to make a sprite dragable 0 Answers
Drag and Drop w/ Snapping 0 Answers
Animation doesn't play all the way through unless holding the key down 1 Answer
A Question about sprites 3 Answers