How to make part follow a Trajectory?

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?

1 Like

I no longer have to use a beam and calculate the trajectory with this code:

local t = 6.5;
local point = workspace:WaitForChild("Point")
local hrp = workspace:WaitForChild("Spawner"):WaitForChild("BoxBody")
local bball = workspace:WaitForChild("Box");

local function createBox(count)
	for i = 1, count, 1 do
		local g = Vector3.new(0, -workspace:WaitForChild("Gravity").Value, 0);
		local x0 = hrp.CFrame * Vector3.new(0, 5, 5)

		-- calculate the v0 needed to reach mouse.Hit.p
		local v0 = (point.Position - x0 - 0.5*g*t*t)/t;

		-- have the ball travel that path
		local c = bball:Clone();
		
		c.Velocity = v0;
		c.CFrame = CFrame.new(x0);
		c.CanCollide = true;
		c.Parent = game.Workspace;
		
		task.spawn(function()
			task.wait(15)
			local explosion = Instance.new("Explosion")
			explosion.BlastPressure = 500000
			explosion.ExplosionType = Enum.ExplosionType.Craters
			explosion.BlastRadius = 20
			explosion.Position = c.Position
			explosion.Parent = workspace
			
			c:Destroy()
		end)
		
		task.wait(0.1)
	end
end

while task.wait(5) do
	createBox(10)
end

However I now have a new problem: The object I want to move is a model, however .Velocity doesn’t work on a model. Anyone know a solution for this?

Model.PrimaryPart?

sdfsdfsdfsdfsfdsdfsfd