Raycasting not finding my object

I want to drag an object from a place to another and OnMouseUp() check if the object is dropped on an object with the “gameArea” tag. Unfortunately the raycast doesn’t detect the object.

private void OnMouseUp()
    {

        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
      
        RaycastHit[] hits;
        Ray camRay = Camera.main.ScreenPointToRay(curPosition);
        hits = Physics.RaycastAll(camRay);
        GameObject lastGameObject = null;

        for (int i = 0; i < hits.Length; i++)
        {
            RaycastHit hit = hits[i];

            if (hit.transform.tag == "gameArea")
            {
                Debug.Log("gameArea");
                lastGameObject = hit.transform.gameObject;
                break;
            }
           
        }
        if (lastGameObject != null && lastGameObject.tag == "gameArea")
        {
            transform.parent = gameArea;
            transform.position = new Vector3(0, 0, 0);
        }
        else
        {
            transform.parent = startingParent;
            transform.position = startingPosition;
            startingParent.GetComponent<CardPositioning>().CardReceived();
        }
    }

The gameArea has a collider and the curPosition is also correct.

I don’t mean for this to sound evasive to your question & code, but have you considered using the IPointer interfaces? There are ones for OnDrag & OnDrop. You can implement them on your drag & drop objects. :slight_smile:

https://docs.unity3d.com/ScriptReference/EventSystems.IDragHandler.html

https://docs.unity3d.com/ScriptReference/EventSystems.IDropHandler.html

No, it’s cool, I’m new to unity so any suggestions for a better approach are very welcome. :slight_smile:

For this to work I need a Canvas as a parent of my objects in the scene, right?

You need an event system. You only need a canvas if you’re using UI items. If they are sprites or 3d objects, the canvas isn’t required. :slight_smile: The interfaces work regardless.

You do need a physics raycaster or physics raycaster 2d , depending if you use either of those, though (in addition to the event system).

Write back if you try it out & say how it goes? :slight_smile:

The OnBeginDrag(), OnDrag() and OnEndDrag() are not being called somehow. I don’t know what I’m doing wrong.

Are you using 3d or 2d?
Do you have (for 3d) a physics raycaster or (for 2d) a physics raycaster 2d in the scene?
Do you have an event system in the scene?

Show the code you’re using?

I’m unsing 3D, I do have an event system in the scene and I use a physics raycaster in the code, but by now the functions are not even called. The logs are not being printed. Right now the code looks like this

public class DragCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("Begin Drag");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("Dragging");
        transform.position = eventData.position;
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("End Drag");
    }

That’s odd. Do you have colliders on the game objects that have these scripts?
note: I do not think you will be able to use eventData.position to set the transform, as that is a screen space coordinate, but you can use the delta to help you move.

Yes, I do.

I’m testing it in a project now, and all’s well (with a small adjustment for movement)…

My entire scene:

  1. camera (with physics raycaster)
  2. event system
  3. cube
  4. script you posted on cube

This is my small update to your code, btw:

public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("Dragging");
        Vector3 wpos = Camera.main.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, Mathf.Abs(Camera.main.transform.position.z - transform.position.z)));
        transform.position = wpos;
    }

Yeah, it works now. I didn’t add the raycaster in the camera. Now it works properly. Thank you.

Ah, awesome! Glad you got it working. :slight_smile: Is it working well for your goals/needs?