Part following another part's path with fixed distance between them

  1. What do you want to achieve?
    I am trying to get a part following another part that is going along a path and i want there to be a fixed distance between them (Only in the x, y and z). And i want the movement to be smooth, i would have used TweeningService but i want the speed at which they move to be dynamic so i can change the speed at anytime. And the parts should be able to move in circles, curves or in a straight line.
    In other words imagine trains wheel where part 1 is the front wheel and part 2 is the back wheel and i want to be able to move part 1 along a track that can go up, down, left or right on a track and have the back wheel move on the same path as the front wheel but with a fixed distance between the centers of the wheels

  2. What is the issue?
    The issue is i don’t know how i can do i have an idea but it just won’t be like i want it to.

  3. What solutions have you tried so far?
    I have tried to search for an answer but with no luck.

Edit: tried to explain it a bit more.

1 Like

i guess i might have asked something that is hard to do or something lol

1 Like

Not really. Your question is super unclear.

Especially this part. A fixed distance in 3D space also needs a direction. Will the end position be the direction from the part’s current position to the target - or will it always be “behind” the target (positive Z axis direction in object-space)?

Then, TweenService would be “smooth” but it would have to be done on the client - you can’t get smooth movement tweening from the server. But then you’ll need replication tactics.

There’s a lot of unknowns here.

1 Like

So the movement is just in the X and Z positions in a straight line from center to center of the parts. Also the tweeningService is just that it makes smooth movements but in either case it just wouldn’t work because as far as i can understand you can change the speed mid way through and just won’t work with keeping the distance when dealing with curved movement

1 Like

Is this something like what you want?

https://gyazo.com/0a5ee804896309e5a7ae09776e0adf0e

If so, here’s the code:

local distance = 3
while true do
	script.Parent.Part2.CFrame = script.Parent.Part2.CFrame:Lerp(script.Parent.Part1.CFrame * CFrame.new(0, 0, distance), 0.1)
	wait()
end

The way this works is it called the Lerp function, which seems to be a better alternative to tweenservice in your case. It basically smoothly moves a number to another number, and roblox has a built in function for lerping CFrames. We can lerp part2’s cframe to x amount of distance behind it, which is positive Z axis. CFrames are part orientated, so thats why it works with orientation and such. If we used position, the part’s orientation would maintain itself and instead just kind of stand behind part1.

2 Likes

it is almost correct as the on difference is the thing it the paths curves
One senario where this wouldn’t work is this


the circle is the path they are following and the parts are where they should be but if i used what you shown they wouldn’t be like it as the 2nd part should move along the path the front part has been moving at

Also the distance is the distance from center to center of the parts

To do that, you need to define the center of the circle and it’s radius. For this, we’re using only X and Y to make things simple. Let’s say its at 0, 0. To find part1’s position, you need to give it angle theta from the top position. In your image, the uppermost part, we’ll call that part1, is 0 degrees away from the topmost point. Your second part, is 90 degrees. Simple right? To calculate the position, we can use trig.

local center = {0, 0}
local radius = 5
do --for part 1
local degrees = 0
local x = center[1] + radius*math.cos(math.rad(degrees))
local y = center[2] + radius*math.sin(math.rad(degrees))
end

do --for part 2
local degrees = 90
local x = center[1] + radius*math.cos(math.rad(degrees))
local y = center[2] + radius*math.sin(math.rad(degrees))
end

but who has time for this boring math??? roblox did it for us in the form of CFrame.Angles!!

local origin = CFrame.new(0,0,0)
local radius = 5
part1.CFrame = origin * CFrame.Angles(0, 0, 0) * CFrame.new(0, 0, radius)
part2.CFrame = origin * CFrame.Angles(0, 90, 0) * CFrame.new(0, 0, radius)
1 Like

Sure but there is a problem with this as well because the path changes all the time sometimes striaght sometimes circular and sometime in an oval and can go between each of these types of paths.
Like this i have marked the path the parts are moving.
Capture

ive got no clue what you need, you’re giving me 0 information and what you’re asking is very general. i’d suggest you look into cframes and stuff first before making a devfourm post

2 Likes

So what it is i want is the second part to follow the same path as the other part but the distance between the center of each part is the same at all times

Why don’t you simply use RocketPropulsion? Check out the tutorial here.

1 Like

The problem with that is that you can’t make it stop at a certain distance and also the fact that you can’t make it follow the exact path of the other part and not to mention the fact that you can’t even manually stop it using a distance thing because the actual position of the part doesn’t change only it’s collision and it visual box is moved

Guess i might just need to have the game be worse since nobody seams to be able to help with exactly the thing i need :confused:

1 Like

So either people just don’t know the answer or the people who have done this before just used some sort of trick and just don’t want to share or just isn’t on dev forum and seing this :frowning:

What I have done recently is have a part that I CFrame a certain distance from the player (2 studs for example), and welded the player’s head, and that part is invisible and noncollidable, and stays in front of the player, but is attached to an object that the player clicks, using AlignPosition and AlignOrientation physics constraints. My code for achieving this is VERY messy, but I think this is something you could try figuring out, using the API cold examples for the physic constraints. As for CFraming the part, just use the lookVector of the player’s head, after they spawn. I would make the part “Massless”, as well, so it doesn’t interfere with the player’s weight.

what you seem to be asking for is what Lumber Tycoon does, when picking up items, and may other games, that use physics for doing this. Also Pet systems have similar mechanics, and there’s even some free pet system kits in free models i’ve seen that use BodyMover physics - you might check there as well.

I believe one system I found was using BodyGyro, and BodyPosition, within a WHILE DO loop to keep a pet a certain distance from a player, using CFrame calculations, to determine the next position the pet should be at, based on its current magnitude (distance), and other pets following the player. Each pet was numbered for tracking, so that it would be placed at a certain point of a circle, around the player.

1 Like

so as far as i can understand is that this will have the part a certain distance from the other part but the problem is it have to follow a curve, circle or a straight line and can change between them when needed. Which this doesn’t seam to be able to do. But it might just be me being dumb at understanding things

If you attach a part using Attachments, using AlignPosition (and maybe AlignOrientation to make sure its facing the right direction if you want) physics constraint, you can have a part or part of a model follow it it any direction. you don’t have to have the part welded to the character, but using CFrame , have the anchored part moved, and then item you want to do the following will “float” over to it.

1 Like