Make a projectile more accurate to the mouse position

example: 2022-09-21 18-48-11 GIF by whereiuploaddevforum | Gfycat

any help apprieciated!

local script:

local uis = game:GetService("UserInputService")
local remote = game.ReplicatedStorage:WaitForChild("Ability")

local mousep = true

uis.InputBegan:Connect(function(k)
	if k.KeyCode == Enum.KeyCode.E then
		
		if mousep == true then
			
			local c = game.Workspace.CurrentCamera
			local mp = uis:GetMouseLocation()
			
			remote:FireServer(true,c:ScreenPointToRay(mp.x,mp.y).Direction)
		else
			remote:FireServer(false)			
		end
	end
end)

Server script:

local remote = game.ReplicatedStorage:WaitForChild("Ability")
local abilities = game.ReplicatedStorage:WaitForChild("Abilities")

local tweensv = game:GetService("TweenService")

local info = TweenInfo.new(10,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)

local goals = {
	Orientation = Vector3.new(0,0,1000)
}

remote.OnServerEvent:Connect(function(plr,isMousePos,mousePos)
	local char = game.Workspace:FindFirstChild(plr.Name)
	
	if char then
		local root = char:FindFirstChild("HumanoidRootPart")
		local hum = char:FindFirstChild("Humanoid")
		
		if root and hum then
			local fireball = abilities.Fire.Fireball:Clone()
			fireball.Anchored = false
			fireball.Parent = workspace
			local fireballtween =  tweensv:Create(fireball,info,goals)
			
			if mousePos then
				fireball.CFrame =  CFrame.lookAt(root.Position, mousePos)
			else
				fireball.CFrame = root.CFrame
			end
			
			fireball.Orientation = fireball.Orientation + Vector3.new(0,-180,0)
			
			local bv = Instance.new("BodyVelocity",fireball)
			bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			
			local speed = 100
			
			if isMousePos then
				bv.Velocity= mousePos * speed
				--bv.Velocity = fireball.CFrame.LookVector*speed
			else
				bv.Velocity= root.CFrame.lookVector * speed				
			end

			fireballtween:Play()
			game:GetService("Debris"):AddItem(fireball,10)
		end
	end
end)

Personally I use

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
mouse.Hit.Position

to get where the player’s mouse is aiming. I think I had to account for UI inset when I was using :ScreenPointToRay(), which might be the issue you are having right now.

2 Likes

do you have any ideas on how i would get a direction from that im not too sure

so i actually found a solution using the mouse position so thanks (mouse.Hit.P) :

				bv.Velocity= -((fireball.Position - mousePos).Unit * speed)

you might not need a minus but for me i did

1 Like