Placement system - help needed!

Hi. I have made a wall placement system for walls similar to bloxburg with raycasting however I want the rotations to snap to 90 degrees and not be slanted. My code:

		local ray = Ray.new(PositionA, (PositionA - PositionB).unit * Mag)
		
		local part, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, returnIgnoreList())

		local distance = (PositionA - position).magnitude
		beam.Size = Vector3.new(ReplicatedStorage:FindFirstChild(Item).Size.Z, 10, distance)
		beam.CFrame = CFrame.new(PositionA, position) * CFrame.new(0, 0, distance / 2) + Vector3.new(0,5,0)
		TempBuild = beam

image

Thank you!

1 Like

you can snap to the world axes by removing any components of the direction that are not the longest direction. So if Z is the longest direction from start point to end point, set the other two components to be the same as the start point.

1 Like

This function worked for me, it returns a CFrame snaped to the closest direction. Both arguments are Vector3 values.

local units = {
	[2] = Vector3.new(1, 0, 0);
	[3] = Vector3.new(1, 0, 0);

	[4] = Vector3.new(0, 0, -1);
	[5] = Vector3.new(0, 0, -1);

	[6] = Vector3.new(-1, 0, 0);
	[7] = Vector3.new(-1, 0, 0);

	[8] = Vector3.new(0, 0, 1);
	[1] = Vector3.new(0, 0, 1);
}

local function get_Beam_CFrame(origin_Pos, end_Pos)
	local delta_Pos = end_Pos - origin_Pos
	local radians = math.atan2(delta_Pos.X, delta_Pos.Z) + math.pi
	local degrees = math.deg(radians)
	local unit_Index = math.ceil(degrees/45)

	local distance = delta_Pos.Magnitude
	local look_Pos = origin_Pos + units[unit_Index]

	local beam_CFrame = CFrame.new(origin_Pos, look_Pos) * CFrame.new(0, 0, distance / 2) + Vector3.new(0,5,0)
	return beam_CFrame
end
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.