Bow Attack Has Problems Shooting Where The Mouse Cursor Is At

So I’m trying to make this attack for my game that’s essentially a bow and arrow, you click at a certain spot on the screen and it will shoot an arrow at where your cursor was.

The problem being though, wherever I click it just seem to shoot at a completely random direction. It is a consistent randomness though as if I keep my cursor in the same spot for multiple shots it will keep shooting in that same randomized direction.

Here is the snippet of code that spawns the arrow, I’ve cut out any unimportant code for brevity’s sake:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr,X,Y,Z)
	if active == 1 then
		active = 0
		local g = game.ReplicatedStorage["Dream Sans"].Arrow:Clone()
		g.owner.Value = script.Parent.Parent
		g.Parent = workspace
		g.PrimaryPart = g.Head
		g:SetPrimaryPartCFrame(script.Parent.Parent["Torso"].CFrame * CFrame.Angles(math.rad(X),math.rad(Y),math.rad(Z))) --Puts the arrow at the owner's torso then change it's direction
		Tool.Enabled = false
		task.wait(1)
		Tool.Enabled = true
		active = 1
	end
end)

And yes the tool itself does have a RemoteEvent inside of it, here are the content’s of the LocalScript inside of it:

script.Parent.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		script.Parent:FireServer(Mouse.Hit.X,Mouse.Hit.Y,Mouse.Hit.Z)
	end)
end)

So far I have tried taking out the ‘math.rad’ parts inside of the direction script with essentially the same results but just with a different randomness if that makes sense. I do believe that the issue lies with the ‘XYZ’ values not actually being directions and moreso specific points at which you click at that go beyond the ideal 180 number for angle values.

Though the question is, how would I go about factoring that into the script though? Any and all help is greatly appreciated, thank you! ^ ^

1 Like

Mouse.Hit is a 3D position, with each component (X, Y, Z) having units of studs.

Your code then tries to treat the X, Y, and Z distances as degrees/angles. Trying to treat a unit of distance as a unit of rotation doesn’t make sense unless you run them through a function like sine or cosine that converts ratios to angles.

What you probably want is to use the CFrame.lookAt constructor, which makes a new CFrame “at” a position pointing towards a location.

-- Set the CFrame of the arrow to one "at" the torso facing towards the Mouse.Hit position
g:SetPrimaryPartCFrame(CFrame.lookAt(script.Parent.Parent["Torso"].Position, Vector3.new(X, Y, Z)))
1 Like

It worked perfectly! Thank you for the help! ^ ^

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.