Ad
Log in to vote
1

Dragging physics like Lumber Tycoon 2?

Asked by
Cuvette 222
11 months 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? LordDragonZord 2819 — 11mo
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 222 — 11mo
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 552 — 11mo
0
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 222 — 11mo
View all comments (2 more)
1
This was what I was trying to ask about the other day... Haha. Inscendio 40 — 11mo
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 40 — 10mo

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

1 answer

Log in to vote
0
Answered by 10 months ago
Edited 10 months 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 337 — 10mo
Ad

Answer this question



Ad