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