Raycast from rectangular room corners to center?

This is an extension of my prior question posted here: Issue with cut off rooms in dungeon generator - Unity Answers

However, this time I have more of a focused question in regards to solving this problem (which, in the end, wasn’t inadvertently solved as I had hoped).

I am in the progress of creating a dungeon generator akin to older dungeon crawler games. The problem, to try and write it simply, is that in the situation in which three or more rooms are generated in the same general area during generation, there is the possibility that the walls created by the first room generated can close off the edges of a room to be built later. There is the wanted effect of rooms generating inside of other rooms, so overall this behavior is intended, however there is a small possibility of small sections of room being completely closed off, which is bad.

I decided a solution to solve the problem is to cast a ray from the corners of a completed room towards the center, and destroy any walls blocking the way. I intend to tweak it more in the future to better preserve the design of dungeons, but to start I need to actually get this working, which it does not.

Below is the current code I am using:

function TestAccess(x:int, z:int, toX:int, toZ:int) {
	var rHit:RaycastHit[];
	var startLoc:Vector3 = Vector3(x, 1, z);
	var endLoc:Vector3 = Vector3(toX, 1, toZ);
	var dist:float = Vector3.Distance(startLoc,endLoc);
	rHit = Physics.RaycastAll(startLoc, endLoc, dist/2);
	for(var o:int = 0; o < rHit.length; o++) {
		if(rHit[o].collider.transform.position.x != x && rHit[o].collider.transform.position.z != z) {
			Destroy(rHit[o].collider.gameObject);
		}
	}
}

Basically, x and z are the corner block, and toX and toZ are the center block. The intent is to create a ray from the corner and have it travel to the center. Anything it collides with is then destroyed, thus opening blocked sections of room.

The issue is that the ray is not properly generating. Some mid-wall blocks are destroyed that are off any possible corner->center angle, and it doesn’t seem to be properly generating the ray. I’m not exactly sure why. I might not have the “direction” Vector3 of the Raycast properly done, but I tried multiple other Vector3 functions and all of them seem to have no effect, so I’m not really sure what I’m doing wrong. I’m not experienced when it comes to Raycasting or engine Physics in general, so I can’t see where the problem is.

Below is a screenshot of a resulting dungeon with that code in place for every room on all four corners.

All of the red circles are locations in which the raycast has failed to function as intended, and destroyed blocks that it should not be destroying. Note the bottom right corner of the screenshot, where it has destroyed a large portion of the corner, and I have absolutely no idea why. I could see the corner-most blocks if my measurements on where to start were off, but why does it go half way up the wall?

Another noticeable problem is the middle-bottom, that looks like a path, however on closer inspection with selecting the floor tile itself I found that it is actually the remaining floor tile that is left after the wall itself is destroyed (different prefabs with different names). It’s definitely not doing what it is supposed to, but I’m not really sure why.

Any assistance in solving this problem would be greatly appreciated. I feel like I’m almost done with the basic dungeon design functions, but this problem is frustrating every time I see it.

I gave up on Raycasting at all in the manner described. Instead, I decided to do the checks in room generation depending on location the blocks are generated at. On the outside edge of the room, I do checks the second a floor is not built because there is already something there, and destroy walls that are in the way. It will clear out the outside rectangle of walls, thus ensuring that rooms are always accessible.

Not the perfect solution but a good one non-the-less, and one I’m willing to work with and tweak to be better. At the very least it’s a solution.