Projectile meant to shoot directly to mouse, shooting infront of player instead

I’m trying to create a bow tool for practice, and I’m having two issues.

My biggest issue is that the projectile flies directly forwards from the player instead of towards the mouse position, a secondary issue is that depending on how far away from the character I click the speed increases or decreases.

Any help is appreciated

Local Script:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	script.Parent.Shoot:FireServer(mouse.Hit.p)
	print("done")
end)

Tool Script:

local debounce = false

local direction 

local debris = game:GetService("Debris")

local replicatedstorage = game:GetService("ReplicatedStorage")

--Getting mouse position when player clicks
script.Parent.Shoot.OnServerEvent:Connect(function(player, mousePos)
	
	direction = mousePos
	
end)

 --Changing the grip when equipped
script.Parent.Equipped:Connect(function()
	
		script.Parent.GripPos = Vector3.new(0,0,0)

end)

script.Parent.Activated:Connect(function(player)
	if not debounce then 
		debounce = true
		
		--add animation eventually
		--wait function of some sort needed to make the animation and action appear connected
		
		local arrow = replicatedstorage.Projectiles.Arrow:Clone() --cloning arrow object to be the projectile launched by bow
		
		arrow.Parent = workspace
		
		arrow.CFrame = script.Parent.Handle.CFrame
		
		print("1")

		--Velocity of the arrow
		
		local bv = Instance.new("BodyVelocity")
		
		bv.MaxForce = Vector3.new(1e8, 1e8, 1e8)
		
		bv.P = 0 --this is the "aggression" of the force used to reach the max speed, play with this variable.
		
		bv.Velocity = direction * 10
		
		bv.Parent = arrow
		
		debounce = false
		
		print("2")
	end
end)

Video of what I described

Place file:
Bow Scripting.rbxl (30.9 KB)

1 Like

Your arrow velocity’s increment/decrement is caused by this line of script, you will have to use the unit vector of the direction and multiply it with the speed value; which should look like this:

bv.Velocity = direction.Unit * 10

BUT still, this line of code will be differentiated from your intentions, because the bow is flying in the direction of the mouse, but in perspective to the world’s root position (0, 0, 0), not the player’s position.
To correct the problem:

bv.Velocity = (direction - player.Character.HumanoidRootPart.Position).Unit * 10

I haven’t tried the code so it might malfunction, if so, let me know.

3 Likes

“Players.KayneWestReal.Backpack.Tool.Script:46: attempt to index nil with ‘Character’ - Server - Script:46” Is what I get back from the console on the second script

You fixed the issue with the speed changing though so I appreciate it

It means player of the code player.Character.Humanoid... is nil, could you post the entire chunk of the code that errors?

I figured it out on my own my bad for keeping this open, I essentially just did what you said.

changed velocity to

bv.Velocity = (direction - script.Parent.Parent.HumanoidRootPart.Position).Unit * 100

and the projectiles CFrame to

arrow.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame
2 Likes