Make arrow fly to mouse cursor position

I am using this code to make arrow fly

flyEvent.OnClientEvent:Connect(function(arrow)
	
	--print(workspace.CurrentCamera.CFrame.LookVector)
	arrow.LinearVelocity.VectorVelocity = (mouse.hit.Position - arrow.Position).Unit * 100
	arrow.LinearVelocity.Enabled = true
	
	--arrow.AssemblyLinearVelocity = (mouse.hit.Position - arrow.Position).Unit * 50 -- workspace.CurrentCamera.CFrame.lookVector * 200
end)

It works fine most of the time, but when I am too close to the object mouse is pointing to, you can see what happens in the video.

Basically i’m looking for a solution to make arrow move where the mouse is pointing.

I tried using CurrentCamer.CFrame, but my Camera is offseted by 5 on Y, so arrow flies lower than mouse cursor is pointing.

2 Likes
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

1 Like

I saw your reply on the previous post. It unfortunately doesn’t give me much clue as to what to do, because my problem is exactly the targetPosition, which you did not give a value.

Currently my script sort of has a targetPosition, and it is Mouse.Hit.Position. My problem occurs when mousehitposition is too close to player, so instead of Mouse.Hit.Position, i need to use something else.

What can i use?

1 Like

so you can use Mouse.Hit.Position in the code i gave above as well as the target position try it see if you get the same problem

1 Like

so do targetPosition = mouse.hit.position?

1 Like

yes you can do

local targetPosition = mouse.hit.position
1 Like

I tried it, as expected, exactly the same problem occured. Arrows fly fine, but when i get close to any object, the same issue as in the video occurs

1 Like

I used to have a similar issue, I recommend raycasting from the mouse location on the screen and using that result.

Here’s an example:

local ray = camera:RayFromPosition(UserInputService:GetMouseLocation()) --not sure about the function name
local raycastResult = workspace:Raycast(ray.Orgin, ray.Direction, RaycastParams)

Note that this is only a part of the code.

Edit: Realized you didn’t want from the mouse position, but the screen middle. I suppose you can get the middle by getting the screen size. You would then put that instead of UserInputService:GetMouseLocation().

1 Like

Thats because its flying to the mouse position like we set in the code

image

1 Like

Yes, I know, as i described the issue, and i am trying to solve this issue. I want the arrow to fly to the screen center

1 Like

but it is flying to the screen centre

1 Like

It is obviously not when i am close to any object

1 Like

GetMouseLocation returns a vector2, with which i cannot operate in 3D

1 Like
1 Like

but that is the screen centre

if you want the arrow to fly straight you would need to change the spawn location

maybe if you spawn the arrow at the characters rootpart then it will fly straight forwards

another option would be to have a over the shoulder camera that aligns with the arrow spawn location

1 Like

I want the arrow to spawn at bows location, then fly towards the screen center, but without the above mentioned issues occuring. Perhaps what is confusing you is the definition of screen center.

I dont want the arrow to fly to the screen center in 2D. I want the arrow to fly straight, but towards where player’s mouse is pointing, so that players can aim better.

1 Like

its currently doing just that flying from the spawn location to the direction on the mouse position in 3D space and the mouse is positioned at the centre of the screen

here is another method that does not do what your asking for but does what i think you want it to do

local speed = 4
flyEvent.OnClientEvent:Connect(function(arrow)
	arrow.LinearVelocity.VectorVelocity = workspace.CurrentCamera.CFrame.LookVector * speed
	arrow.LinearVelocity.Enabled = true
end)

this will make the arrow fly straight but not to the screen centre in 3D space

1 Like

and if you shift the camera over to the right

1 Like

I did do it with currentcamera look Vector, and with that arrow was moving perfectly fine, no matter where the character was. The issue with it is that i am offseting the camera, and because of that arrows fly lower than mouse cursor is, because camera look vector becomes lower than the actual screen middle

1 Like

the thing is what your asking for is a paradox you want the arrow to fly in the direction the camera is facing but at the same time you want the arrow to fly to the mouse hit position
but them 2 directions are not the same so what your asking for is impossible

in the image above i show by shifting the camera so its over the arrow it will make the camera lookvector and mouse.hit.position align so that the arrow fly’s the way you want

if you don’t want to move the camera then we can move the hit position forward by 1000 while this is not the mouse.hit.position it might give a effect that your trying to achieve

local speed = 4
flyEvent.OnClientEvent:Connect(function(arrow)
	local targetPosition = mouse.Hit.Position + mouse.Hit.lookVector * 1000
	local direction = (targetPosition - arrow.Position).Unit
	arrow.LinearVelocity.VectorVelocity = direction * speed
	arrow.LinearVelocity.Enabled = true
end)

you can also change the 1000 value maybe something like 100 or 10 might be better

2 Likes