How to make My Projectile more accurate with the mouse?

Hey, people of the forum! I was wondering how I can make my Projectile actually accurate with the mouse position. I have it so when you press R a fireball gets launched to where your mouse is pointing, but where it gets shot is nowhere accurate to where the mouse is!

Ill provide the code and a video of what happens.

Here is The code:

local fb = game.ReplicatedStorage:WaitForChild('FireBall')

local Debris = game:GetService('Debris')
local TS = game:GetService('TweenService')

local Animations = script:WaitForChild('Animations')
local Meshes = script:WaitForChild('Meshes')

local speed = 75

local damage = 50

fb.OnServerEvent:Connect(function(player,Direction)
	local Char = player.Character
	local Hum = Char:WaitForChild('Humanoid')
	local Root = Char:WaitForChild('HumanoidRootPart')
	
	local Wave = Meshes:WaitForChild('Wave'):Clone()
	
	local FXFolder = Instance.new('Folder',workspace)
	FXFolder.Name = player.Name..' FX'
	Debris:AddItem(FXFolder,2)
	
	Wave.CFrame = CFrame.new(Root.CFrame * CFrame.new(0,0,-3).p, Direction.p) --Root.CFrame * CFrame.new(0,0,-3)
	Wave.Parent = FXFolder
	Wave:SetNetworkOwner(nil)
	
	Wave.Touched:Connect(function(Hit)
		if Hit:IsA('BasePart') then
			if not Hit: IsDescendantOf(Char) then
				local Humanoid = Hit.Parent:FindFirstChild('Humanoid')
				
				if Humanoid then
					if Humanoid.Health > 0 then
						FXFolder:Destroy()
						Humanoid:TakeDamage(damage)	
					end
					
				else
					FXFolder:Destroy()
					
				end
				
			end
		end
	end)
	
	local Ring = Meshes:WaitForChild('Ring'):Clone()
	
	Ring.CFrame = Wave.CFrame
	Ring.Parent = FXFolder
	Ring:SetNetworkOwner(nil)
	
	local weld = Instance.new('ManualWeld')
	weld.Part0 = Ring
	weld.Part1 = Wave
	weld.C0 = weld.Part0.CFrame:toObjectSpace(weld.Part1.CFrame)
	weld.Parent = weld.Part0
	
	local velocity = Instance.new('BodyVelocity',Wave)
	velocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	velocity.Velocity = Direction.LookVector * speed --Root.CFrame.LookVector * speed
	
	fb:FireClient(player)
end)

Looks like its wanting to spawn a bit to the left, so try adding an offset to it. Something like:

Wave.CFrame = CFrame.new((Root.CFrame + CFrame.new(3,0,0)) * CFrame.new(0,0,-3).p, Direction.p)

Im likely incorrect with how thats formatted, but it should get the idea out.

1 Like

What’s the code that determines the value of Direction? I think analyzing this should help solve the issue. Although, it may also be related to what @Glitching_Dreams suggests. I’m not the most familiar with CFrames, but maybe the CFrame.new(0, 0, -3) in your code is incorrect. I assume the goal is to make the wave appear 3 studs in front of the player, but maybe the x-axis should have -3 in it instead of the z-axis. I’m not sure though.

The problem is probably with how you found Direction on the client. The server sided code here isn’t really causing that issue.

Only thing I see is that here you’re treating Direction like a world space CFrame here, using the CFrame.lookAt() constructor format.

But here you’re treating Direction like an object space CFrame, and the orientation looks correct in the video.

The velocity vector has to be from the Fireball’s starting position, in the direction of the world space position of the mouse. The video suggests you found that vector from the camera’s position instead of the fireball’s starting position.

What determines Direction is Mouse.Hit.

So yeah; exactly what I said:

The orientation of Mouse.Hit is relative the Camera.CFrame. You need to apply a velocity vector that’s relative the fireball’s starting position.

velocity.Velocity = (Wave.Position - Direction.Position).Unit * speed
1 Like