Help on making an dragging system where the part snaps depending on the cursor target

Hey! So im trying to make an game similar to Obby Creator, but instead of just obbies, you can build whatever you want with an editor similar to roblox studio’s, and im currently working on the placement system

i already got some of the script done but i dont know how to make the parts snap to what part your cursor is, like in the video shown below:

Heres my current script:

local Module = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local Player = game:GetService("Players").LocalPlayer
local PlayerMouse: Mouse = Player:GetMouse()

local CommonData = ReplicatedStorage:WaitForChild("CommonData")
local Parts = CommonData:WaitForChild("Parts")
local PlacingEssentials = Parts:FindFirstChild("PlacingEssentials")

local Grid = workspace.Grid

local function CalculatePosition(X, Y, Z)
    local GridUnit = 1

    local function RoundToGrid()
        return Vector3.new(math.floor(X / GridUnit + 0.5)*GridUnit, math.floor(Y / GridUnit + 0.5)*GridUnit, math.floor(Z / GridUnit + 0.5)*GridUnit)
    end
    
    return RoundToGrid()
end

function Module:PlacePart(Config)
    local Part = Config.Part:Clone()
    local SelectionBox = PlacingEssentials:FindFirstChild("SelectionBox"):Clone()
    
    PlayerMouse.TargetFilter = Part
    
    Part.Parent = workspace.Storage.Temp
    SelectionBox.Parent = workspace.Storage.Temp
    SelectionBox.Adornee = Part
    
--// By the way this part below (the position changing) is just an test prototype that i had made 
    Part.Position = CalculatePosition(PlayerMouse.Hit.X, PlayerMouse.Hit.Y, PlayerMouse.Hit.Z)
    local c
    c = RunService.RenderStepped:Connect(function()
        TweenService:Create(Part, TweenInfo.new(0.1, Enum.EasingStyle.Quint), {Position = CalculatePosition(PlayerMouse.Hit.X, PlayerMouse.Hit.Y, PlayerMouse.Hit.Z)}):Play()
    end)
end

return Module

I’m not experienced enough with scripting to help you with any specifics, but I have an idea. Cast a ray from the camera at the angle of the cursor from the center of the screen (center is camera orientation, all the way right is camera orientation + FOV), and when that ray hits something, move the part to the intersection. Drawing below:

1 Like

Ima try experimenting with this, if it works ima mark your post as a solution

Glad it worked! I’m happy I could help :>

1 Like

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