How to make arrow fly to mouse position

So i have this script that makes arrow fly. It works perfect, arrows fly where player looks, as intended.

flyEvent.OnClientEvent:Connect(function(arrow)
	arrow.AssemblyLinearVelocity =  workspace.CurrentCamera.CFrame.lookVector * 200
end)

The problem is, in one of the scripts, i offset the camera by Y. And i want the arrows to fly where player’s mouse is, so he can aim better. But they fly lower than the cursor, because camera offset is set to +2 by Y.

How can i make the arrow fly where the mouse is?

1 Like
local Speed = 200
local Mouse = game.Players.LocalPlayer:GetMouse()
arrow.AssemblyLinearVelocity = (Mouse.hit.Position - arrow.Position).Unit*Speed
2 Likes

Thank you! :smiley: This works as intended

This seemed to work at first, but gets broken once i approach any character, or any object, as you can see in the video

The arrow’s Can Collide is always false, and some of the objects Can Collide is true, some false, but the issue occurs with any object. It looks like it happens when mouse hit position is too close to player.

Can you please help me solve this?

First use CFrame.lookAt(). Then tween the arrow to go to it’s look vector multiplied by the radius from the mouse position and arrow

Or with linear velocity set it to look straight to the mouse position and switch on its velocity then turn off its velocity once the radius is close enough.

If you want it to have a constant speed

Ex:

Speed = 100
TimeTakes = radius (ex: 1000)

radius / Speed = 10

Velocity = 10

Haven’t tried it yet, will try tomorrow, but i am 90% sure that the same issue would still occur, because this problem happens when mouse hit position is too close to player, so setting to look at mouse hit position would make the arrow again fly weird.

The solution can be to use something else instead of mouse.hit.position, but i dont know what.

And i don’t want to use tweens, since i will be doing other manipulations with projectiles too, and tweens would make them far too complicated

If you mean like the arrow going side ways instead of nailing to the ground its because of where the arrow spawned + where the mouse is aiming at(AKA the Instance its hovering onto), what you can do is instead of basing it on the arrow u can base it on the Rootpart Instead, I’m not sure if this will fix that problem thought and not sure if there’d be some aim inaccuracy.

local Speed = 200
local Mouse = game.Players.LocalPlayer:GetMouse()
arrow.AssemblyLinearVelocity = (Mouse.hit.Position - HRP.Position).Unit*Speed
local arrow = ...
local targetPosition = ...

``` a vector3 describing the direction to the target position
local direction = targetPosition - arrow.Position

-- change this ratio to any number you like to change the flight curve
local ratio = 200 / direction.Magnitude

-- if you want to know how long it will take the arrow you get to its target
-- local throwTime = 1 / ratio 

-- work out the force using gravity so it hits perfectly
local force = direction * ratio + Vector3.new(0, game.Workspace.Gravity * 0.5 / ratio, 0)

-- set the arrows velocity
arrow.AssemblyLinearVelocity = force

here is a demo of the code above
https://www.roblox.com/games/7918396847

you can use “CFrame” to control arrow movement better
Cframe

Try this out

local Speed = 200
local Mouse = game.Players.LocalPlayer:GetMouse()
local Camera = game.Workspace.CurrentCamera
arrow.AssemblyLinearVelocity = (Mouse.hit.Position - arrow.Position).Unit*Speed + Camera.CFrame.LookVector * 10