- Home /
UI Collision Detection
Hello. I would like to show within a canvas element some pictures, which can be moved by drag n` drop. Here, a collision detection to take place, which should prevent the images are placed over each other. I've already tried with Rigidbody2D and BoxCollider2D but unfortunately without success. I hope that who can help. My code currently looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragAndRotateHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
private bool drag;
public void OnDrag( PointerEventData eventData )
{
if (drag)
{
transform.position = eventData.position;
}
}
public void OnPointerDown( PointerEventData eventData )
{
drag = true;
}
public void OnPointerUp( PointerEventData eventData )
{
drag = false;
}
private void OnCollisionEnter( Collision other )
{
Debug.Log("collission enter");
}
private void OnCollisionEnter2D( Collision2D collision )
{
Debug.Log("collission enter 2D");
}
private void OnCollisionStay( Collision other )
{
Debug.Log("collission stay");
}
private void OnCollisionStay2D( Collision2D collision )
{
Debug.Log("collission stay 2D");
}
private void OnCollisionExit( Collision other )
{
Debug.Log("collission exit");
}
private void OnCollisionExit2D( Collision2D collision )
{
Debug.Log("collission exit 2D");
}
}
Comment