How is Physics.Raycast works

Hello there, my main problem is that I do check for collider between player and target to see if player can interact and it’s working fine untill you hit an edge of the collider.

As you can see on pictures for some reason it can interact when you at the edge of the wall while raycast still should hit the wall (cyan line in scene view). Does it have minimum steps to on ray when checking for colliders or do I miss something?
alt textalt text
The code itself for checking looks this way

startPoint = new Vector3(transform.position.x, transform.position.y + 0.8f, transform.position.z);
endPoint = new Vector3(c_NearestTarget.transform.position.x, startPoint.y, c_NearestTarget.transform.position.z);

if(Physics.Raycast(startPoint, endPoint, minInteractionDistance, 1 << 11))
return false;

return true;

Where:
startPoint = position above player;
endPoint = position above target;
minInteractionDistance = minimum interaction distance to be able interact;
1 << 11 = layer of wall.

I just realised while writing the answer below that you used Raycast and passed a worldspace position as second argument where a worldspace direction is needed. If you want to work with two positions you should use a Linecast instead.


I don’t quite get your logic of your start and end points. It’s hard to tell where your player cube is located relative to the wall. A side view would have been useful. Also we don’t know the scale. You add “0.8” to the start height which if the cube is a standard cube (which has an extent of 0.5) is actually a bit above the player cube. Also your end point is not the enemy but the point on the same height as the player above the enemy. So if that start point is peaking slightly over the wall you could get all sorts of results.

Also keep in mind that you have reverse logic in your raycast. That means if you don’t have a hit you can interact. Since you setup a minInteractionDistance, that means if the wall is outside that range, you actually can’t hit the wall and you return true.

We don’t see how and where you draw that cyan line.