Before I start, I'm not asking for a script given to me at all. Just merely concepts or ideas.
But I'm sure most of you have played Defaultios, Lumber Tycoon 2. The dragging system is great and would be really useful in a new game of mine that I'm currently developing. I've tried multiple ways of going around it and all of them are quite poor. Such as when a player grabs a block it moves it 5 studs above the floor and allows them to drag it, and when they released it the block goes back down 5 studs.
It's a really bad way of going about it I know, but I can't quite put my finger on it. Anyways, if you got any ideas or examples or even snippets if possible, that would be great!
Many thanks, Cuvette
It seems to be a pretty simple system. You've probably noticed the little blue sphere that appears at your mouse click point. From what I can see, it looks like that sphere is welded to whatever part you click on, at that point. The sphere then is given a BodyPosition and BodyGyro, which update to place the part in the correct direction and orientation.
So, we can keep a variable of the sphere and a function to create a unique one:
local dragSphere; local function CreateDragger() if (dragSphere) then dragSphere:Destroy(); end dragSphere = Instance.new("Part", workspace); -- If FilteringEnabled is on, this could be a little more complex to implement dragSphere.Shape = "Ball"; dragSphere.Size = Vector3.new(); -- Smallest size possible dragSphere.CanCollide = false; local bp = Instance.new("BodyPosition", dragSphere); bp.MaxForce = Vector3.new(10000, 10000, 10000); local bg = Instance.new("BodyGyro", dragSphere); bg.MaxTorque = Vector3.new(10000, 10000, 10000); return bp, bg; end
Then, when the moues clicks something, we do something like:
local distance = (mouse.Hit.p - characterPosition).magnitude; local bp, bg = CreateDragger(); dragSphere.CFrame = CFrame.new(mouse.Hit.p); local weld = Instance.new("Weld"); weld.Part0 = mouse.Target; weld.Part1 = dragSphere; weld.C0 = mouse.Target.CFrame:inverse() * dragSphere.CFrame; weld.Parent = dragSphere; local rotOffset = CFrame.new(characterPosition, mouse.Hit.p):inverse() * dragSphere.CFrame;
Once we've attached ourselves, we can do this every frame:
bp.Position = characterPosition + (mouse.Hit.p - characterPosition).unit*distance; bg.CFrame = CFrame.new(characterPosition, mouse.Hit.p) * rotOffset;
Of course, a bit more implementation needs to be done to create a fully working system, but that's the main gist of it I think.
If you wanna see-able for other players w/ keybinding included but you have to make it so it inserts a sphere in
Keybindable Code: local player = game.Players.LocalPlayer local mouse = player:GetMouse() local camera = workspace.CurrentCamera local RunService = game:GetService("RunService") local target local down local Keybind = "E"
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode[Keybind] then if mouse.Target.Name == "Grab" then if mouse.Target ~= nil and mouse.Target.Locked == false then target = mouse.Target mouse.TargetFilter = target down = true target.CanCollide = false target.Anchored = false
local gyro = Instance.new("BodyGyro") gyro.Name = "Gyro" gyro.Parent = target gyro.MaxTorque = Vector3.new(5000000, 5000000, 5000000) local force = Instance.new("BodyPosition") force.Name = "Force" force.Parent = target force.MaxForce = Vector3.new(100000000000, 100000000000, 100000000000) end end end
end)
game:GetService("RunService").RenderStepped:Connect(function() if down == true and target ~= nil then target.Gyro.CFrame = target.CFrame target.Force.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 5) end end)
game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode[Keybind] then if target then if target:FindFirstChild("Gyro") or target:FindFirstChild("Force") then target.Gyro:Destroy() target.Force:Destroy() target.CanCollide = true target.Anchored = false end end down = false mouse.TargetFilter = nil target = nil end end)
NonKeybindable Code: local player = game.Players.LocalPlayer local mouse = player:GetMouse() local camera = workspace.CurrentCamera local RunService = game:GetService("RunService") local target local down
mouse.Button1Down:connect(function() if mouse.Target.Name == "Grab" then if mouse.Target ~= nil and mouse.Target.Locked == false then target = mouse.Target mouse.TargetFilter = target down = true target.CanCollide = false target.Anchored = false
local gyro = Instance.new("BodyGyro") gyro.Name = "Gyro" gyro.Parent = target gyro.MaxTorque = Vector3.new(5000000, 5000000, 5000000) local force = Instance.new("BodyPosition") force.Name = "Force" force.Parent = target force.MaxForce = Vector3.new(100000000000, 100000000000, 100000000000) end end
end)
game:GetService("RunService").RenderStepped:Connect(function() if down == true and target ~= nil then target.Gyro.CFrame = target.CFrame target.Force.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 5) end end)
mouse.Button1Up:connect(function() if target then if target:FindFirstChild("Gyro") or target:FindFirstChild("Force") then target.Gyro:Destroy() target.Force:Destroy() target.CanCollide = true target.Anchored = false end end down = false mouse.TargetFilter = nil target = nil end)