- Home /
 
Touch drag 2D object in x axis
Hi!
I am really new to mobile development and I need some help with something. I want my 2D* object to move when it is dragged by the player. You have to tap on the object and then drag it for it to work. Now my problem is that I don't know how I can implement this. Any help would be appreciated!
*It is a 2D object with 2D physics, therefore I can't use a raycast. If I were to use 3D physics it would break the game A LOT.
Thanks in advance!
Are you using the new UI, got several drag scripts already just need to know which one might help you out best.
@$$anonymous$$mmpies I am using the UI but not for the object I need to be moved. The object I need to be moved is a sprite.
Answer by Mmmpies · Feb 07, 2015 at 02:59 PM
Not sure with Sprites as 2D isn't something I'm familiar with but this works with UI elements.
 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;
             }
 
         }
     }
 }
 
               Hope it helps, you will need to restrict the y axis if you only want z but, hopefully, it'll give you a head start.
Hi , I want to drag my 2D object in x or y axis , I want that when I touch the 2D object it should move left-right or up-down and not diagonally. Can I achieve this with you script?
Your answer
 
             Follow this Question
Related Questions
Drag-and-drop dot on mobile 2D 0 Answers
Simultaneous Touch Drag Controls 0 Answers
Help With Touch to Drag Script 1 Answer
Touch drag objects, raycast cant detect object? 1 Answer
2D UI Button When Touched Image 0 Answers