Egomoose comes in clutch for this twice over, he also has some easy code to modify in his projectile motion tutorial:
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 = hrp.CFrame * Vector3.new(0, 2, -2)
local v0 = (mouse.Hit.p - x0 - 0.5*g*t*t)/t;
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)
(See the first section of the beamProjectile
section)
This guide was originally written for scriptinghelpers . The original can be found here .
As a somewhat active member of the Scripting Helpers discord one of the most common questions I see is how to have a projectile travel an arc. Most people want to know how to do this for things like basketballs or cannon balls and so forth. Since this is such a popular question I thought it would be worth writing a blog post on it and talking about a few other things we can extend from our findings.
Deri…