2D Drag And Drop Sprites Stalling?

So i’m working on being able to drag and drop sprites clean and efficiently. I’ve got it almost perfect, but when i drag one of my sprites on top of another 1 or 2 sprites (at a moderately reasonable speed) the sprite i was dragging stalls out and i have to touch it again to pick it back up. Also, one thing worth mentioning is that when a sprite is dropped, it should jump to its original starting position. This is feature functions correctly UNLESS i drag it through other sprites and it stalls.

TLDR; I can drag my sprites, nice and smooth no problems and when i lift my finger to release the sprite, it goes back to its designated home position. BUT when i drag my sprite through other sprites, it gets stuck and has to be manually moved away from other sprites in order to function correctly.

I’ve got 3 scripts you’ll need to know about.

  • MasterScript, (Attatched to an empty gameobject called “Master”) I’m mostly just using this to help store time sensitive/object specific variables.

    public class MasterScript : MonoBehaviour {

      public bool canDrag = true;
      public string currentTile = "";
    

    }

  • TouchDrag, This script is on EVERY sprite that i want to drag. It controls what happens when i touch is detected and directly references to this next script.

    public class TouchDrag : TouchSprite {

      Vector3 startPos;
      string ct;
      bool ms;
      public SpriteRenderer sp;
    
      void Start () {
          startPos = gameObject.transform.position;
          ms = GameObject.Find("Master").GetComponent<MasterScript>().canDrag;
          ct = GameObject.Find("Master").GetComponent<MasterScript>().currentTile;
      }
      
      void Update () {
          TouchInput(GetComponent<BoxCollider2D>());
      }
    
      void OnFirstTouchBegan() {
          if (ms) {
              if (ct == "") { 
              ms = false;
              ct = gameObject.name;
              sp.sortingOrder = 10;
              }
          }
      }
    
      void OnFirstTouch() {
              if (gameObject.name == ct) {
                  Vector3 pos;
                  pos = new Vector3(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).x,
                  Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).y, transform.position.z);
                  transform.position = pos;
              }
      }
    
     void OnFirstTouchEnded() {
          transform.position = startPos;
          ms = true;
          ct = "";
          sp.sortingOrder = 4;
      }
    

    }

  • TouchSprite, This script is all of the necessary framework for my previous TouchDrag script to function properly. It’s pretty much just for defining when certain events and triggers occur.

    public class TouchSprite : MonoBehaviour
    {

      public static bool guiTouch = false;
    
      public void TouchInput(BoxCollider2D collider)
      {
          if (Input.touchCount > 0)
          {
              if (collider == Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)))
              {
                  switch (Input.GetTouch(0).phase)
                  {
                      case TouchPhase.Began:
                          SendMessage("OnFirstTouchBegan", SendMessageOptions.DontRequireReceiver);
                          SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
                          guiTouch = true;
                          break;
                      case TouchPhase.Stationary:
                          SendMessage("OnFirstTouchStayed", SendMessageOptions.DontRequireReceiver);
                          SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
                          guiTouch = true;
                          break;
                      case TouchPhase.Moved:
                          SendMessage("OnFirstTouchMoved", SendMessageOptions.DontRequireReceiver);
                          SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
                          guiTouch = true;
                          break;
                      case TouchPhase.Ended:
                          SendMessage("OnFirstTouchEnded", SendMessageOptions.DontRequireReceiver);
                          guiTouch = false;
                          break;
                  }
              }
              if (Input.touchCount > 1)
              {
                  if (collider == Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)))
                  {
                      switch (Input.GetTouch(0).phase)
                      {
                          case TouchPhase.Began:
                              SendMessage("OnSecondTouchBegan", SendMessageOptions.DontRequireReceiver);
                              SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
                              break;
                          case TouchPhase.Stationary:
                              SendMessage("OnSecondTouchStayed", SendMessageOptions.DontRequireReceiver);
                              SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
                              break;
                          case TouchPhase.Moved:
                              SendMessage("OnSecondTouchMoved", SendMessageOptions.DontRequireReceiver);
                              SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
                              break;
                          case TouchPhase.Ended:
                              SendMessage("OnSecondTouchEnded", SendMessageOptions.DontRequireReceiver);
                              break;
                      }
                  }
              }
          }
      }
    

    }

I’d greatly appreciate it if you’re able to help! Thanks it advance!!!

EDIT: I’ve continued working on this, and it seems like i might need to create a new base that detects when im already holding a sprite. I thought i could just do it through the DragSprite.cs but it’s proving more and more difficult. Any help would be greatly appreciated!

Any advice would be appreciated. Even a hint.