IDropHandler only trigger when it's SpriteRenderer sortingOder is higher than draggable sortingOder
Hello Everyone, I'm Implementing a simple Drag&Drop system that works with SpriteRenderer. I know implementing this with UI System has no trouble and there is many tutorial about it but I insist to make it with Sprite Renderer .
Using Unity 2019.4.21f1 LTS
Here is the situation : I Wrote a Simple Script for Dragging Sprite
using UnityEngine;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
Vector3 startPosition;
private Camera _mainCamera;
private void OnEnable()
{
_mainCamera = Camera.main;
}
public void OnBeginDrag(PointerEventData eventData)
{
//TODO : Check for Enable/Disable dragging input
startPosition = transform.position;
}
public void OnDrag(PointerEventData eventData)
{
Vector2 draggingPos = _mainCamera.ScreenToWorldPoint(eventData.position);
transform.position = draggingPos;
}
public void OnEndDrag(PointerEventData eventData)
{
transform.position = startPosition;
}
}
Then I wrote a simple script for dropping sprite:
using UnityEngine;
using UnityEngine.EventSystems;
using System;
public class Slot : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
Debug.Log($"{eventData.pointerDrag.name} Dropped on {name}");
}
}
Then I did these :
attached Slot.cs to a gameobject with SpriteRenderer , BoxCollider2d
attached Draggable.cs to a gameobject with SpriteRenderer , BoxCollider2d
attached a PhysicsRaycaster2D to camera
add an EventSystem to scene
Every thing is working fine if Slot object Sprite Renderer sorting order is greater than Draggable Sprite Renderer sorting order or is in higher priority sorting layer. but I want that the Draggable be on top. any suggestion will be appreciated. Thanks