Hello, the question is basically in the title. I want to make a part that travels along a curved path at a constant speed no matter the distance of p0 and p2. I tried searching the forum but couldn’t find any solution.
pls help
Hello, the question is basically in the title. I want to make a part that travels along a curved path at a constant speed no matter the distance of p0 and p2. I tried searching the forum but couldn’t find any solution.
pls help
You’ll need to use TweenService or lerping. Personally I use TweenService as it is much easier imo.
To start off, I assume you’re using a Bezier curve because you mentioned “p0 and p2”.
To travel along a Bezier curve, you’ll need a loop.
Firstly, create an empty “Model” inside the part. Then define the “parent” of the generated parts as “script.Parent.Model
”.
After doing so, you’ll need to create a table where the parts will be put into, so that the system knows what part it should tween to. In this guide, it’ll be “local SystemPoints = {}
”, but you can always name it to whatever you want.
Next, you need to put the parts from the model into that table. For that, you’ll need to define a variable and get the model’s children.
In this guide, it’ll be “local pointsChildren = script.Parent.Points:GetChildren()
”
Then create an advanced loop that inserts the children into the table through that variable. In this guide it’ll be:
for i, v in pairs(pointsChildren) do
table.insert(systemPoints,v)
end
Next, define your tween information. In this guide it’ll be:
local tweenInfo = TweenInfo.new(
0.05, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.In, -- EasingDirection
0, -- RepeatCount (when less than zero the tween will loop indefinitely)
false, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
The time determines how fast it’ll tween from one part to the other. I recommend setting it to 0.05 or lower in this case, otherwise it’ll take a long time to tween.
Now you can get to tweening the part. For that, you’ll need to create a loop that’ll travel up the table to find the parts, and add their positions and orientations into a table, and then will play it. In this guide, it’ll be:
for i = 1, #systemPoints do
local Goal = {}
Goal.Position = systemPoints[i].Position
Goal.Orientation = systemPoints[i].Orientation
local tween = tweenService:Create(script.Parent,tweenInfo,Goal)
tween:Play()
tween.Completed:Wait() -- waits for the tween to be finished
end
Pretty much that’s how I would do it. If you have any questions, feel free to ask.