I am using Modeling a projectile’s motion - Resources / Community Tutorials - Developer Forum | Roblox as a guide on how to make a part that will go to a position in a parabolic way.
So far I have wrote this code:
local t = 1;
local point = workspace:WaitForChild("Point")
local startPoint = workspace:WaitForChild("Part")
local attach0 = Instance.new("Attachment", game.Workspace.Terrain);
local attach1 = Instance.new("Attachment", game.Workspace.Terrain);
local beam = Instance.new("Beam", game.Workspace.Terrain);
beam.Attachment0 = attach0;
beam.Attachment1 = attach1;
local function beamProjectile(g, v0, x0, t1)
-- calculate the bezier points
local c = 0.5*0.5*0.5;
local p3 = 0.5*g*t1*t1 + v0*t1 + x0;
local p2 = p3 - (g*t1*t1 + v0*t1)/3;
local p1 = (c*g*t1*t1 + 0.5*v0*t1 + x0 - c*(x0+p3))/(3*c) - p2;
-- the curve sizes
local curve0 = (p1 - x0).magnitude;
local curve1 = (p2 - p3).magnitude;
-- build the world CFrames for the attachments
local b = (x0 - p3).unit;
local r1 = (p1 - x0).unit;
local u1 = r1:Cross(b).unit;
local r2 = (p2 - p3).unit;
local u2 = r2:Cross(b).unit;
b = u1:Cross(r1).unit;
local cf1 = CFrame.new(
x0.x, x0.y, x0.z,
r1.x, u1.x, b.x,
r1.y, u1.y, b.y,
r1.z, u1.z, b.z
)
local cf2 = CFrame.new(
p3.x, p3.y, p3.z,
r2.x, u2.x, b.x,
r2.y, u2.y, b.y,
r2.z, u2.z, b.z
)
return curve0, -curve1, cf1, cf2;
end
game:GetService("RunService").RenderStepped:Connect(function(dt)
local g = Vector3.new(0, -game.Workspace.Gravity, 0);
local x0 = startPoint.CFrame * Vector3.new(0, 2, -2)
local v0 = (point.Position - x0 - 0.5*g*t*t)/t;
--print(mouse.Hit.p)
local curve0, curve1, cf1, cf2 = beamProjectile(g, v0, x0, t);
beam.CurveSize0 = curve0;
beam.CurveSize1 = curve1;
-- convert world space CFrames to be relative to the attachment parent
attach0.CFrame = attach0.Parent.CFrame:inverse() * cf1;
attach1.CFrame = attach1.Parent.CFrame:inverse() * cf2;
end)
This code creates a beam which serves as the trajectory.
Example:
How can I move a part to follow this trajectory?