5
votes

I have a GameObject that implements IDragHandler and IDropHandler from UnityEngine.EventSystems.

In OnDrop, I want to check, has this object been dropped in front of another object. I'm using Physics.Raycast, but it only returns true in some cases. I use screen point to ray as direction for my ray, and this transform as origin for the ray of the Raycast.

My code:

 public void OnDrop(PointerEventData eventData)
 {
         var screenRay = Camera.main.ScreenPointToRay(new Vector3(eventData.position.x, eventData.position.y, 0.0f));
         var thisToObjectBehind = new Ray(transform.position, screenRay.direction);
         Debug.DrawRay(thisToObjectBehind.origin, thisToObjectBehind.direction, Color.yellow, 20.0f, false);
         RaycastHit hit;
         if (Physics.Raycast(thisToObjectBehind, out hit))
         {
             Debug.LogFormat("Dropped in front of {0}!", hit.transform);
         }
 }

I'm using a perspective camera. When the object is dropped in front of objects straight forward from the screen/camera, Physics.Raycast sometimes returns true, but "hit" contains this, not the object behind this. Sometimes it returns false. None of these two results are expected, there are objects behind this that should be possible to Raycast.

When the object is dropped in front objects in the outskirts of the camera view, Physics.Raycast succeeds in finding the objects behind.

The Debug ray looks fine, it is drawn from the object I dropped, backwards to the object it should hit.

1
Note: If I instead of using screenRay set thisToObjectBehind = new Ray(transform.position, transform.forward * -1), the Raycast succeeds, so there shouldn't be anything wrong with the colliders. However, I don't want to limit myself to this having to be facing the camera.Helena
what is the eventData value when succeed and fail?Bizhan
Do you not need a LayerMask?Fredrik Schön
Fredrik is absolutely correct, layers was the issue.Helena

1 Answers

1
votes

Temporarily placing the object that was dropped in the IgnoreRaycast layer solved my issue. This also meant that I could skip to set the ray's origin to transform.position.

    public void OnDrop(PointerEventData eventData)
    {
        // Save the current layer the dropped object is in,
        // and then temporarily place the object in the IgnoreRaycast layer to avoid hitting self with Raycast.
        int oldLayer = gameObject.layer;
        gameObject.layer = 2;

        var screenRay = Camera.main.ScreenPointToRay(new Vector3(eventData.position.x, eventData.position.y, 0.0f));
        RaycastHit hit;
        if (Physics.Raycast(screenRay, out hit))
        {
            Debug.LogFormat("Dropped in front of {0}!", hit.transform);
        }

        // Reset the object's layer to the layer it was in before the drop.
        gameObject.layer = oldLayer;
    }

EDIT: Removed try/finally blocks from code snippet due to peformance reasons.