Make arrow fly to mouse cursor position

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

I understand, thank you for the detailed response. I actually tried offseting mousehitposition but to no avail, didnt try it the way you just showed, let me try and come back

The issue in the video disappeared, but arrow once again flies lower than the mouse cursor is. Perhaps we could change just the Z offset of mouse hit position? Or maybe make it fly to currentCameralookVector, but again with a given offset. It is just unclear to me how to apply offset to a lookvector, since it is not a normal vector3, but rather weird decimals that i have no clue how are calculated

this should be a mix of the cameras look vector and the direction to the mouse hit

local speed = 4
flyEvent.OnClientEvent:Connect(function(arrow)
	-- get the cameraUpVector
	local cameraUpVector = workspace.CurrentCamera.CFrame.UpVector

	-- get the cameraRightVector
	local cameraRightVector = workspace.CurrentCamera.CFrame.RightVector

	-- get the directionUpVector
	local directionLookVector = (mouse.Hit.Position - arrow.Position).Unit
	local directionRightVector = cameraUpVector:Cross(directionLookVector)
	local directionUpVector = directionLookVector:Cross(directionRightVector)

	-- use the cameras right vector and the directions up vector to workout a new vector
	local direction = cameraRightVector:Cross(directionUpVector)

	arrow.LinearVelocity.VectorVelocity = direction * speed
	arrow.LinearVelocity.Enabled = true
end)
1 Like

Thank you so much for your consistent effort, I think this will work, unable to try right now since im at another location than the computer. Anyway marking as solution for now. I’ll try tomorrow, and if this doesnt work, ill mark as solution your answer that it is impossible.

Which is the reason for the first variable. It converts the 2D position to a 3D ray.

This gives error at line

local directionLookVector = (mouse.Hit.Position.Y - arrow.Position.Y).Unit

Attempt to index number with unit

Oops forgot to remove the .Y from the positions

I updated the message with the correct code