Placement system

I have a function that updates a model’s position based on the mouse position using a raycast. I want to add a way to detect when the raycast hits a wall. If it hits a wall, I want the part to be moved to the ground directly below that wall instead of sticking to the wall surface.

How would I find the ground position beneath that wall and place the object there

hopefully that makes sense If anyone has any ideas for this I’d really appreciate it.

function PlacementController:update()
	local mouse = LocalPlayer:GetMouse()
	local char = self.tool.Parent
	local root = char:FindFirstChild("HumanoidRootPart")
	local cam = Workspace.CurrentCamera
	
	local hitPos
	
	if not root then return end
	
	local mouseRay = cam:ScreenPointToRay(mouse.X, mouse.Y)
	local WorldHit = Workspace:Raycast(cam.CFrame.Position, mouseRay.Direction * 500)
	
	if not WorldHit then return end
	
	if WorldHit.Normal.Y < 0.5 then 
		--WHAT IM FOCUSION ON IS HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	end
	
	hitPos = WorldHit.Position
	
	local direction = (root.Position - hitPos).Unit
	direction = Vector3.new(direction.X, 0, direction.Z).Unit
	
	local groundResult = Workspace:Raycast(hitPos + Vector3.new(0,200,0), -Vector3.yAxis * 500)
	
	if not groundResult then return end
	
	local normal = groundResult and groundResult.Normal or Vector3.yAxis
	
	local right = direction:Cross(normal).Unit
	local forward = right:Cross(normal).Unit
	
	local target = CFrame.fromMatrix(hitPos, right, normal, forward) * CFrame.new(Constants.TOOL_MODEL_VISUALIZATION_OFFSET)
	
	self.visualizationModel.PrimaryPart:PivotTo(self.visualizationModel.PrimaryPart.CFrame:Lerp(target, 0.2))
	
	--YOU CAN IGNORE EVERYTHIN

	local touching = Workspace:GetPartsInPart(self.visualizationModel.Model.CollisionBox)

	self.isModelColliding = false
	
	for _, part in touching do
		if not part:IsDescendantOf(self.visualizationModel) and part.Name ~= "Baseplate" then
			self.isModelColliding = true
			break
		end
	end

	local box = self.visualizationModel.Model.CollisionBox
	box.Color = self.isModelColliding and Constants.COLLISION_BOX_COLLIDING_COLOR or Constants.COLLISION_BOX_CLEAR_COLOR
end
1 Like

so as I see ur focusing on

if not WorldHit then end

part?

whoops mb made an error, corrected it.

ima kinda late :\
is anything under wall a part or a terrain? Like, what is the result of raycast?
srry for that typa questions, didnt get it fully :*)

1 Like

sorry i got the problem figured out. also I apologize for the post altogether lol it was pretty messy.

1 Like

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