Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Dragging physics like Lumber Tycoon 2?

Asked by
Cuvette 246 Moderation Voter
8 years ago

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

0
We don't understand. I've never played Lumber Tycoon and I don't feel like playing a game just to fix your problem, do you have a better explanation? EzraNehemiah_TF2 3552 — 8y
0
It's literally been front page for about 4 months but anyways, say you chop down a tree. You can then drag it into your truck it kinda floats and follows the mouse. But if you move the mouse too fast or the piece of wood is too heavy it will drop. Hopefully that is a better explanation. Cuvette 246 — 8y
0
I'm not sure about your level of experience in coding but I'm gonna say that you probably don't have nearly enough experience to make something like this. Though like I said. I'm not sure what your experience level really is. AZDev 590 — 8y
1
I could probably do it with weeks of trial and error and ive created a similar system within a couple of days. But i really wanna ace it and get it down to the last detail. I can script it if i just have an idea on where to start and what methodologies to use. Cuvette 246 — 8y
View all comments (2 more)
1
This was what I was trying to ask about the other day... Haha. Inscendio 42 — 8y
0
You notice that when you click on whatever thing, a very small tiny sphere appears at the mouse position of where you clicked it. Inscendio 42 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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.

0
As for the whole rotating in midair thing, that's a simple matter of changing the BodyGyro's CFrame by some amount when the right key is pressed. nicemike40 486 — 8y
0
Where to put those things and are they complete? Trueorfalse123321 0 — 5y
Ad
Log in to vote
0
Answered by 4 years ago

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)

Answer this question