3D Raycast has trouble finding moving object (?) [SOLVED]

Hello, 3D Raycast works strangely and I don’t understand why. The setup is very simple, the enemy object from which the raycast goes to the player is blocked by a Box3D object + a debug object showing the position of the raycast intersection. If Enemy detects player it rotates.

Please note that when the raycast intersects with a wall, intersection detection occurs sequentially every frame, without problems or breaks. But for some reason, problems arise with detecting the player’s object; it detects it seemingly randomly, sometimes he hangs at one point, sometimes he seems to be shooting a beam from somewhere other than the enemy’s position, etc.

What could be wrong?
Thank you

I keep testing different options to find out the possible cause, but I just get more confused.
For some reason, 3DRaycast only works if the XY position of the target is less than the XY position of the ray source. It properly detects only what is to the left and above the ray source (when viewed from above). Any position of the player to the right or below the source causes bugs, it either does not see the player at all, or detects him in a buggy inconsisten way.
Yet again, it detects wall intersections perfectly, but not the player.

Why are you raycasting on an angle (enemy.z + 32 to player.z)? And I assume you’ve kept the origin at the top left (if looking down onto the object), which makes me wonder if that is causing the raycast to miss the object altogether. That could also explain why the detection is happening only when the target is below the ray source.

I would suggest raycasting from and to the centre of objects, so you know it’s not going to glance or narrowly miss the corner.


The +32 angle is random, I was just trying different variations where the source is above/below the target to see if any particular ray position/angle was causing the problem.

I tried changing the coordinates to object.centerX()/object.centerY() and it works like a charm, thank you.

For some reasone I just assumed that origins for Box3D are automatically placed at their bounding box centers since there are no setting for it like for 2D objects.