Creating a dragging system

I want to create a system where you could drag items and move or rotate them, ideally close to what the select tool in studio does, but without visible snapping. My first thought was doing something like this:

local Player = game.Players.LocalPlayer

local Mouse = Player:GetMouse()

local Part = Instance.new("Part",workspace)

Mouse.TargetFilter = Part

game:GetService("RunService").RenderStepped:Connect(function()

    Part.Position = Mouse.Hit.p

end) 

But then, when my mouse points at the baseplate, the part gets halfway stuck, like this:

Then, I thought about adding half the height of the part to compensate.

Part.Position = Mouse.Hit.p + Vector3.new(0,Part.Size.Y,0)
Result:


As you can see, it doesn’t get stuck in the ground anymore. But, what about other sides? This could happen in a room:

As you can see, the part also gets stuck, but not on the y-axis, but rather on the x- and z- axes.

How do I determine how much to move back, on which axes?

i’d say either raycasting or :GetTouchingParts(), but can’t really think of how to do it exactly

And then there are models, which can be oddly shaped! Should I move them accordingly using Model:GetBoundingBox()?

Thanks in advance!

yes i’ve grinded google for about 14 hours but i couldn’t find any that does this easily

2 Likes

This resource should help:

4 Likes

I’ve read that before, but that doesn’t solve the problem of parts getting halfway stuck.

Have you gone through the basics like:
1. Is the part anchored?
2. Is there another script stopping it from running?

If you’re simply trying to avoid collisions, use a BodyPosition inside the part being dragged instead of setting the Position property. You can even use a BodyGyro to keep it upright and you can adjust the properties of both of the body movers to get tweening of the movement.

If you really don’t want to use a BodyPosition, you can use the normal returned from the raycast to tell which direction the face the part will be on is facing and then you can offset the part from that surface using the size of the model, the normal of the surface, and the position of the collision.

Using bodymovers might actually get the part stuck in corners or behind walls, so I opted to go for raycasting

Will try this out, thank you

1 Like

can you send the working script?

4 Likes