Help with Raycast position

Hi! I have a problem with my generation of parts, I don’t know how to change the positions of my parts so that each one is well positioned.

My script basically what it does is that it obtains an appropriate position for the part in a random way and then it is supposed to place it in front of the wall but it does not, I think the error is in this part of the script (Line 22 here):

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Part, IgnoreForRaycast}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if raycastResult then
	local hitPart = raycastResult.Position
	local hitPartName = raycastResult.Instance.Name
	local hitInstance = raycastResult.Instance

	if hitPartName ~= "Wall" and hitPartName == "Floor"then
		local raycastParams2 = RaycastParams.new()
		raycastParams2.FilterDescendantsInstances = {Part, IgnoreForRaycast} 
		raycastParams2.FilterType = Enum.RaycastFilterType.Blacklist
		local raycastResult2 = workspace:Raycast(hitPart, Vector3.new(19,0,19), raycastParams2) 


		if raycastResult2 then
			local HitPart2 = raycastResult2.Instance.Position
			local part = Instance.new("Part")
			part.Parent = game.Workspace.GameMainSystem.Invisible
			part.Position = HitPart2  --I think the problem is here, i dont know how to position each part depending on where did it spawn
			part.Size = Vector3.new(13.4, 7, 11.2)
			part.Anchored = true
			part.Name = "InvisibleRandom"
			table.insert(PartsTable, part)
		end
	end


		
			
end


Since I get the raycast I guess the position of the raycast somehow places the part in the middle of the walls, does anyone have any kind of solution?
:confused:

1 Like

I haven’t worked with Raycasts a lot, but I think you are just finding the Position of the Part that the Raycast hits. That’s why your new Parts get stuck in the exact center of the Part they hit.

If you look at Raycasting | Roblox Creator Documentation you’ll see how to calculate the exact Position that the ray actually hits in the last section of the script.

Instead of getting the position of the wall (RaycastResult.Instance.Position), get the position of where the ray intersected with the wall (RaycastResult.Position). Use that position to position the object.

3 Likes