Rocket Projectile Doesn't look at destination

I am trying to learn vectors and physics on roblox studio so I decided to script a rocket launcher, but when i launch it there are 2 bugs.

1- the rocket kills the player that shot the rocket
2- the rocket spasms out and starts spinning while moving

here’s the script:

local tool = script.Parent
local Handle = tool.Handle

local rs = game:GetService("ReplicatedStorage")
local rocketEvent = rs.RocketServer
local swoosh = rs.PlaySwoosh
local rocketsound = tool:WaitForChild("Swoosh")
local boom = tool.Boom
local lifespan = 30
local rocketSpeed = 100 -- Constant speed of the rocket

swoosh.OnServerEvent:Connect(function(plr)
	rocketsound.Looped = true
	rocketsound:Play()
end)

rocketEvent.OnServerEvent:Connect(function(plr, raypos, raydist)
	local Projectile = rs.ProjectilePart:Clone()
	local gravity = workspace.Gravity
	local projmass = Projectile:GetMass()
	local CounterGrav = gravity * projmass

	local att = Instance.new("Attachment")
	att.Parent = Projectile
	att.Name = "VectorAttachment"

	local vf = Instance.new("VectorForce")
	vf.RelativeTo = Enum.ActuatorRelativeTo.World
	vf.Parent = Projectile
	vf.Attachment0 = att
	vf.Force = Vector3.new(0, CounterGrav, 0)

	Projectile.Anchored = false
	Projectile.Parent = workspace

	local direction = (raypos - Handle.Attachment.WorldCFrame.Position).Unit
	Projectile.AssemblyLinearVelocity = direction * rocketSpeed
	Projectile.CFrame = CFrame.lookAt(Handle.Attachment.WorldCFrame.Position, raypos)

	game:GetService("Debris"):AddItem(Projectile, lifespan)

	Projectile.Touched:Connect(function(hit)
		local exp = Instance.new("Explosion")
		exp.Parent = workspace
		exp.Position = hit.Position
		rocketsound:Stop()
		rocketsound.TimePosition = 0

		boom:Play()
		Projectile:Destroy()
	end)
end)
1 Like

For one, to fix the projectile killing the player you could do 2 things: make a safe filter or just spawn the projectile further out.
I would prefer a safe filter, but the other option is easier as you just add a vector to the initial CFrame. Basically where you set CFrame.lookAt you’d also add a local forward position (which is a unit vector of length 1 so multiply it by a desired number) assuming your handle attachment is oriented correctly. If it isn’t oriented, either orient it or find which vector gives you the desired outcome (LookVector, UpVector or RightVector).
A safe filter would basically mean “don’t trigger rocket if it touches the shooting player’s character”. In your code that’d be checking if hit is a part that belongs to the character.

For the spasms there can be many problems so you have to share more in-depth information on how you’ve set it up. For example i’m not sure, but i think CFrame.lookAt makes sure the front is the -Z local axis.

There could also be problems with raypos and raydist not being where they should and instead hitting things in the wrong place.

1 Like