What am I doing wrong?

I’m trying to corner blocks the MC way. I’m not sure how to put this with words but I’ll provide an image showing what I’m trying to achieve.

I’ve made this function so far: Basically defining faces and casting rays from those points. The main issue here is that the faces shift as you rotate the main block(block which will be cornered) I’m thinking that the issue would be resolved if I use the rotation relative to the world rather than the rotation relative to the object itself? I’m just quite unsure how to achieve that

I really hope this makes sense, if not feel free to ask questions.

function Placement:CheckAngle(self, IterateOnce)  - - - Ignore self, that’s the block that we will be moving

	local IgnoreList = {self}
	local HitTable = {}
	local Table = 
for Index, Value in pairs(Table) do
	 local newRay 

     local newCFrame = self.PrimaryPart.Position
	 if Value == "Front" then
		  newRay = Ray.new(newCFrame, Vector3.new(0, 0, -1)*Grid)
	 elseif Value == "Back" then
		 newRay = Ray.new(newCFrame, Vector3.new(0, 0, 1)*Grid)
	 elseif Value == "Right" then
		newRay = Ray.new(newCFrame, Vector3.new(1, 0, 0)*Grid)
	 elseif Value == "Left" then
		newRay = Ray.new(newCFrame, Vector3.new(-1, 0, 0)*Grid)

	 local hit, position, surfaceNormal = game:GetService("Workspace"):FindPartOnRayWithIgnoreList(newRay, IgnoreList) 
	 if hit and hit.Parent and hit.Parent.ClassName == "Model" then  
	 table.insert(HitTable, Value)
	if (HitTable[1] == "Left" and HitTable[2] == "Back") or (HitTable[2] == "Left" and HitTable[1] == "Back") then
                print ("Cornered") 
		return "Corner_1"
1 Like

Just a note, you aren’t doing the front. You’re just checking for angles with the back.

EDIT: Also, I have no clue what you’re actual issue is…

I’m making a placement system and I’m thinking of adding a feature where you can corner blocks just like in MC(Minecraft). At the moment I’m trying to find a way to keep the rays a constant (relative to the world position) without rotating with the block(I also rotate the block when I press R so I don’t want that to happen)

Regarding your solution, I would also add players etc. to the Ray’s ignore list so characters wouldn’t be able to trigger corner blocks.

You might want to consider this:
Skip the raycasting, try to find whether or not blocks are adjacent to the block you are placing by using your grid system. Use the position of the block you just placed, add/subtract the grid size on each axis to get the coordinates of the blocks you want to check. Raycasting could work too, for example when your grid system doesn’t support the aforementioned, but either way you want to get all the adjacent blocks.

Once you make a table of the adjacent blocks, you have to make your own algorithm that picks the right model depending on how many blocks are adjacent (1 adjacent, 2 adjacent, 3, etc.). This would result in fence-like behaviour in MC. You’d include a rotation check as well that rotates in steps of 90 degrees.

As a side-note, depending on what block you are using, it might be important to run the algorithm a second time for the blocks that are adjacent to the placed block. This is important because placing block A down can cause adjacent block B to change shape and turn into a corner as well. Illustrated: image

I noticed you mentioned you don’t want to rotate the model, I would advice against that since it will force you to make a bunch of ‘unnecessary’ extra models. And rotation would be easier.


Really appreciate the answer thank you!