Hey yall! I’m fairly new to projectile-based development and I’ve encountered an issue with calculating an accurate aim direction when shooting an item.
Currently I’ve tried Camera:ScreenPointToRay(mouse.X, mouse.Y, 0) and Mouse.UnitRay, but alas, they both produce this result:
You’ll notice that the direction I’m getting doesn’t hit where I’m aiming. It does eventually reach my cursor if I shoot it into the air and let it progress ~300 studs or so, but I’d like to calculate a direction that intersects the green point that I showed above
I tried adjusting the depth of the ScreenPoint, no luck
I’m just curious how exactly I go about producing a more accurate direction, since I already feel the direction has some arbitrary value to it anyway (is it supposed to intersect when the distance reaches 500 studs? not too sure how roblox even gets this direction in the first place)
I’m not entirely inexperienced with trigonometrical functions and vector math, so don’t hold back if you want to provide a more sophisticated explanation/solution
But regardless, thanks for reading. I look forward to anyone with an answer : )
PS: I’ve spotted this perceived inaccuracy with a lot of legacy gear items as well, but most shooter games on roblox seemingly don’t have this issue, pretty weird.
Another thing I wanted to mention is this isn’t a screen inset issue. I’m using ScreenPointToRay and not the Viewport equivalent
Yeah I can promise you it has nothing to do with ScreenPointToRay. I always have perfect accuracy in my FPS projects but I can’t just share those places without it being stolen. It’s very high quality. If you share your code we can help you fix it
local part = workspace.Part
local mouse = game.Players.LocalPlayer:GetMouse()
-- Event to move part when clicking
mouse.Button1Down:Connect(function()
local unitRay = mouse.UnitRay
local rayOrigin = unitRay.Origin
local rayDirection = unitRay.Direction * 500 -- Set a max distance for the raycast
-- Use a Raycast to determine the exact position in the 3D world
local params = RaycastParams.new()
params.FilterDescendantsInstances = {workspace} -- Adjust filter as needed
params.FilterType = Enum.RaycastFilterType.Blacklist
local result = workspace:Raycast(rayOrigin, rayDirection, params)
if result then
local targetPosition = result.Position
part.AssemblyLinearVelocity = (targetPosition - part.Position).Unit * 50 -- Move toward the target
end
end)