Question by
elbe13 · Jul 07, 2021 at 05:56 PM ·
raycastdrag-and-droppointer
Dragging object, raycast blocked by object
Hi. i Have a problem. When i drag a card over a Spawn Area, eventData.pointerCurrentRaycast.gameObject.tag in some parts over the spawn area prints the correct tag of a spawn area, but in other places prints the tag of a card itself, like the card is blocking the ray. How can I fix it?
public class CardDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private GameObject draggedCard;
private GameObject spawnedUnit;
private Camera mainCam;
private void Awake()
{
mainCam = Camera.main;
}
public void OnBeginDrag(PointerEventData eventData)
{
draggedCard = gameObject;
LoadUnit(draggedCard.GetComponent<Card>().GetCardUnit());
}
public void OnDrag(PointerEventData eventData)
{
draggedCard.transform.position = Input.mousePosition;
if(eventData.pointerCurrentRaycast.gameObject !=null)
Debug.Log(eventData.pointerCurrentRaycast.gameObject.tag);
}
public void OnEndDrag(PointerEventData eventData)
{
if (spawnedUnit != null && eventData.pointerCurrentRaycast.gameObject.CompareTag("SpawnArea"))
{
Vector3 worldPoint = mainCam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mainCam.transform.position.z));
Destroy(draggedCard);
Instantiate(spawnedUnit, worldPoint, Quaternion.identity);
}
}
private void LoadUnit(string unitToSpawn)
{
Addressables.LoadAssetAsync<GameObject>(unitToSpawn).Completed += OnLoadDone;
}
private void OnLoadDone(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> obj)
{
spawnedUnit = obj.Result;
}
}
Comment
Your answer
Follow this Question
Related Questions
Arrows pointing to offscreen enemy 3 Answers
Physics Raycaster Bug? 1 Answer
Have a component ignore all other overlapping raycasts for PointEnter and Exit events? 0 Answers
How to use Raycast to scan everything under dragged 2d object to specify where it can be placed? 2 Answers
GraphicRaycaster.Raycast doesnt work properly while executing OnEndDrag(...) 1 Answer