#Part floating to camera when raycasting a preview (like a tower defense game)

1 messages · Page 1 of 1 (latest)

subtle pine
#

cant figure out why whenever the part is making a preview it just floats to teh camera

#
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local RunService = game:GetService("RunService")

local previewModel = nil
local updating = false

local function createPreview()
    if previewModel then
        previewModel:Destroy()
    end

    previewModel = Instance.new("Model")
    previewModel.Name = "Preview"
    previewModel.Parent = workspace

    local originalHandle = tool:FindFirstChild("Handle")
    local primary = nil

    for _, child in ipairs(tool:GetChildren()) do
        if child:IsA("BasePart") then
            local clone = child:Clone()
            clone.Anchored = true
            clone.CanCollide = false
            clone.Transparency = 0.5
            clone.Parent = previewModel

            for _, desc in ipairs(clone:GetDescendants()) do
                if desc:IsA("Weld") or desc:IsA("WeldConstraint") or desc:IsA("Motor6D") then
                    desc:Destroy()
                end
            end

            if originalHandle and child.Name == "Handle" then
                primary = clone
            elseif not primary then
                primary = clone
            end
        end
    end

    if primary then
        previewModel.PrimaryPart = primary
    end
end

#

hold on still sending sciropt

twin path
#

the raycast is hitting the preview part

subtle pine
#

local function updatePreview()
    if not previewModel or not previewModel.PrimaryPart then
        return
    end

    local cam = workspace.CurrentCamera
    local origin = cam.CFrame.Position
    local direction = (mouse.Hit.Position - origin).Unit * 100

    local excludeList = {player.Character}
    for _, descendant in ipairs(tool:GetDescendants()) do
        if descendant:IsA("BasePart") then
            table.insert(excludeList, descendant)
        end
    end

    local raycastParams = RaycastParams.new()
    raycastParams.FilterType = Enum.RaycastFilterType.Exclude
    raycastParams.FilterDescendantsInstances = excludeList

    local result = workspace:Raycast(origin, direction, raycastParams)

    if result then
        previewModel:SetPrimaryPartCFrame(CFrame.new(result.Position))

        local isWater = result.Instance and result.Instance.Name == "Water"
        local color = isWater and Color3.fromRGB(0, 255, 0) or Color3.fromRGB(255, 0, 0)

        for _, part in ipairs(previewModel:GetDescendants()) do
            if part:IsA("BasePart") then
                part.Color = color
            end
        end
    end
end

local function startPreview()
    if updating then return end
    updating = true
    createPreview()
    RunService:BindToRenderStep("PreviewUpdate", Enum.RenderPriority.Camera.Value + 1, updatePreview)
end

local function stopPreview()
    updating = false
    RunService:UnbindFromRenderStep("PreviewUpdate")
    if previewModel then
        previewModel:Destroy()
        previewModel = nil
    end
end

tool.Equipped:Connect(startPreview)
tool.Unequipped:Connect(stopPreview)
twin path
#

set canquery to false in addition to cancollide=false

subtle pine
subtle pine
#

oh it works tysm

#

but wait

#

why is it in the ground

subtle pine
#

i mean parts

#

oh wait i think ik how to do that