ScreenPointToRay Isn't accurate enough?

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:

image
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 : )

2 Likes

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 :slight_smile:

1 Like

the code obviously doesn’t look exactly like this, but it’s an accurate showcase of the calculation I use

while alive do
 AssemblyLinearVelocity = mouse.UnitRay.Direction * speed
end

Does it look something like this?

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

Awesome, looks like it was because I didn’t adjust the UnitRay direction at all
I appreciate your help, thanks !

1 Like

You’re welcome :slight_smile: I am glad I could help!

1 Like