How to move a part in only one direction when making a custom dragging tool?

I am making a custom dragging tool for my game and one feature that it has is to drag a part in only one direction, say only in the x axis. However, I am at a loss on how I should do this. I have considered using either Vector Mathematics or Prismatic constraints, however I am not entirely sure. Does anyone have any suggestions?

How are you dragging the part in the first place? Are you using the physics engine or CFrames?

Edit:
If you are using physics then you would try using the AlignPosition Force.

CFrames then I think this would be a good resource:

Hi, I’ve read previously about the thread that you have linked. However, the main problem with this thread is that it uses Roblox Handles to move the part, which mean that you can reference the handles in your script without having to worry about direction. In my case, the dragging will be done without handles, which means one of my main concerns is how to move the object in the direction of the line that I want.

I will be using CFrames and Position properties for the tool.

Hmm check out these resources:

And specifically this tool:

based on your implementation you could do

part.Position = Vector3.new(new_X_Position, part.Position.Y, part.Position.Z)

if you want to move it in 1 axis along a line you can do

part.Position = Vector3.new(new_X_Position, line.Position.Y, line.Position.Z)

Interesting! What would line refer to in this case though? I am also looking to make it move about an angled line which is not parallel to the x, y or z axis respectively though, how would that change the code?

line

refers to the line object which you want the part to move on
as for making it move in an angled line, if you want it to exactly an angled line you can change the X and Z axis, adding to the X axis will move it left and adding the Z axis will move it downward (subtracting would be reverse), example:

--// a += b is syntax sugar for a = a + b
part.Position += Vector3new(0.5, 0, 0,5) --//to move left and downward (based on WorldPosition)
part.Position -= Vector3new(0.5, 0, 0,5) --//to move right and upward (based on WorldPosition)
--//I'm not much experienced when it comes to Vector maths, I'm certain there are much better ways to handle and explain this, sorry

if you want to freely move the part in any line excluding the Y axis you can do

part.Position = Vector3.new(new_X_Position, part.Position.Y, new_Z_Position) --//this will only lock the Y axis from changing

If you want to drag it around in an angled line you first have to define a line with a position vector and a direction vector which can be done by finding two points that you want.

This new line will be where the part will be constrained to. Then you can find the closest distance on the line to the mouse position to achieve dragging functionality.

1 Like