63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
![]() |
using System;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.EventSystems;
|
||
|
|
||
|
public class DraggableCard : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
|
||
|
{
|
||
|
private Vector3 originalPosition;
|
||
|
private CanvasGroup canvasGroup;
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
canvasGroup = GetComponent<CanvasGroup>();
|
||
|
}
|
||
|
|
||
|
public void OnPointerDown(PointerEventData eventData)
|
||
|
{
|
||
|
originalPosition = transform.position;
|
||
|
canvasGroup.alpha = 0.5f;
|
||
|
}
|
||
|
|
||
|
public void OnDrag(PointerEventData eventData)
|
||
|
{
|
||
|
transform.position = Input.mousePosition;
|
||
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
|
RaycastHit hit;
|
||
|
if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Ground") // Ground 태그로 변경하였습니다.
|
||
|
{
|
||
|
canvasGroup.alpha = 0; // 마우스가 Ground 위에 있을 때 투명도 변경
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
canvasGroup.alpha = 0.5f;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void OnPointerUp(PointerEventData eventData)
|
||
|
{
|
||
|
canvasGroup.alpha = 1;
|
||
|
|
||
|
// 지형에 올바르게 드롭되지 않으면 원래 위치로 되돌림
|
||
|
if (!IsDroppedOnTarget())
|
||
|
{
|
||
|
transform.position = originalPosition;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private bool IsDroppedOnTarget()
|
||
|
{
|
||
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
|
RaycastHit hit;
|
||
|
|
||
|
if (Physics.Raycast(ray, out hit))
|
||
|
{
|
||
|
if (hit.collider.tag == "Ground")
|
||
|
{
|
||
|
Destroy(gameObject);
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|