Strange behavior doing 2D Raycast inside of OnTriggerEnter2D

I’m trying to very roughly find the contact point between two 2D trigger colliders using a raycast since triggers don’t give me contact points. I will look into using non-triggers as well but I’d like to know if someone can explain why this is happening with my current trigger setup.

I have a ‘player’ physics object, moved with velocity only. I’m colliding it with a non-moving kinematic ‘obstacle’ object. Both have RigidBody2D and 2D colliders. Both 2D colliders are set to be triggers.

Here’s what I have attached to the obstacle.

{
// player object and this script parameter should have layermask set to the same layer in the inspector
    public LayerMask layerMask; 
       
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.tag == "Player")
        {
            Debug.DrawLine(transform.position, other.transform.position, Color.yellow, 2f);
            RaycastHit2D hit = Physics2D.Raycast(transform.position, other.transform.position, Mathf.Infinity, layerMask);
            Debug.Log("RAYCAST HIT: " + hit.collider.ToString());
        }
    }
}

The trigger works as expected, but hit.collider is sometimes found by the raycast and sometimes returns null depending on the position of the obstacle collider and whether the ‘player’ makes contact from top or bottom.

I’ve attached a package with my setup so you can see the behavior. Hit play on the project as is, with the obstacle positioned off to the side with position.x = 4 and the raycast will only find the collider if the player object has passed the obstacle’s midpoint, and only when the player makes contact on the top of the obstacle (raycast from underneath always returns null).

If you set the obstacle position.x to zero, the behavior changes. The collision again is only detected when the player is over the obstacle, but only when it is near the obstacle midpoint. When it gets some distance away, the raycast no longer finds the player.

Any help or insight as to what is happening is appreciated, thank you!

6661951–761974–testTriggerRaycast.unitypackage (4.01 KB)

I’ve had issues in the past raycasting from within one collider trying to hit another. If this is what is happening, one way I resolved it was disabling the collider I was inside of, doing the raycast, then turning the collider back on. This is ugly and could get really ugly if you’re inside a bunch of colliders to start with…

If you mean the raycast always finds the collider it is inside of first, I’m getting around that by using the layermask so that it ignores itself. But yeah turning the collider on and off can get hairy, I’d like to avoid needing to hack it too much if possible. When you did your fix, did you need to advance the frame before trying the raycast?

No, nothing like that. It was a long time ago so it’s entirely possible it was my own bug, but here was what I remember:

  • I was using layers to exclude myself
  • raycasting from inside me was not 100% reliable against nearby ground
  • when I set .enabled to false (on me) and did that raycast, it became 100% reliable

I stuck with that fix. I can’t recall which project it even was or I’d grep you some code…

@Kurt-Dekker Ah understood. I tried a some things based on your suggestion but couldn’t get that to work for my setup. I only need the initial contact (not sustained) and I wanted to use the raycast to place a ‘hurt’ contact spark at the collision location, but I’ve got a workaround I’m satisfied with without using the raycast now. Thanks a lot for your help and time!

I’d still be interested in understanding why my initial setup had such odd results if anyone can explain…

1 Like