How to make a projectile follow a mouse

Hello developers,

I am trying to make a projectile follow a players mouse. I have the entirely of the system scripted already but I think my math is off for the BodyVelocity and I don’t exactly know the right calculations. I really need some help!

SERVER:

--// Services
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--// Items
--# Variables

--# Objects
local AssetsFolder = ReplicatedStorage:WaitForChild('Assets')
local blastPart = AssetsFolder:FindFirstChild('BlastPart')
local blastSound = AssetsFolder:FindFirstChild('Sound')

--# Remotes
local BlastEvent = AssetsFolder:FindFirstChild('Blast')
local MouseEvent = AssetsFolder:FindFirstChild('Mouse')

--# Values
local currentProjectile
local settingPos = false

--# Function
local blastMouse

--// Scripts
BlastEvent.OnServerEvent:Connect(function(Player, Status)
	if Status == true then
		blastSound:Play()
		local newBlast = blastPart:Clone()
		newBlast.Parent = Player.Character:WaitForChild('RightHand')
		newBlast.Position = Player.Character:WaitForChild('RightHand').Position
		
		local newBody = Instance.new('BodyVelocity', newBlast)
		newBody.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		newBody.P = 1000
		newBody.Name = 'Mover'
		
		currentProjectile = newBlast
		settingPos = true
		blastMouse = MouseEvent.OnServerEvent:Connect(function(Player, MousePos)
			local RootPart = Player.Character:WaitForChild('HumanoidRootPart')
			if (MousePos.Position - RootPart.Position).Magnitude < 50 then
				newBody.Velocity = MousePos.Position
			end
		end)
	end
end)

CLIENT:

--// Services
local ContextActionService = game:GetService('ContextActionService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Players = game:GetService('Players')

--// Items
--# Variables
local Player = Players.LocalPlayer

--# Objects
local AssetsFolder = ReplicatedStorage:WaitForChild('Assets')

--# Remotes
local BlastEvent = AssetsFolder:FindFirstChild('Blast')
local MouseEvent = AssetsFolder:FindFirstChild('Mouse')

--# Values
local DB = false


--// Scripts
--# Blast
local function blast(_, input)
	if input == Enum.UserInputState.Begin then
		if not DB then
			BlastEvent:FireServer(true)
			DB = true
		end
	else
		BlastEvent:FireServer(false)
	end
end

-- Bind
ContextActionService:BindAction('Blast', blast, false, Enum.KeyCode.R)

--# DN
BlastEvent.OnClientEvent:Connect(function(Status)
	if Status == false then
		DB = false
	end
end)

--# Mouse Pos
while wait(0.05) do
	local Mouse = Player:GetMouse()
	MouseEvent:FireServer(Mouse.Hit)
end

Thanks!

1 Like

I don’t really have time to provide a great solution for you other than just recommending you add the delta of the camera to some sort of angularvelocity; but there is something else I want to let you know. You shouldn’t be updating this to the server. Give the client network ownership over the rocket and then update it on the client.

1 Like