How to accurately get an origin and direction to fire a projectile at?

This is what Roblox’s default superball uses, condensed into two lines. I’m using FastCast (raycast projectile) with 200 velocity and equivalent gravity deceleration. The original superball uses part where the velocity was target*200 but they are both innacurate anyway, with the superballs sometimes flying into random places.

TargetPos = LocalPlayer:GetMouse().Hit.Position

Origin: c.Head.Position+(targetPos-c.Head.Position).Unit*5
Direction: targetPos-c.Head.Position

I thought hold on what’s all that in the origin. Doesn’t matter right. Also changed to hrp

Origin: c.HumanoidRootPart.Position
Direction: (targetPos-c.HumanoidRootPart.Position).Unit

Also tried doing the method where you manually fire a raycast to get where your mouse is in the 3d space, on recommendation of AI. It didn’t work; the superballs still sometimes decide go off to wherever they liked, conveniently always when a target is in the crosshair, and as per usual AI blamed me.

Anyone know the magic code so the projectiles won’t randomly decide to go up and backwards or way off to the side when it shouldn’t? Tyyyyyyy

1 Like

The issue comes from Mouse.Hit being unreliable. Use a camera raycast instead

local ray = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 1000)
local target = result and result.Position or (ray.Origin + ray.Direction * 1000)
local direction = (target - character.Head.Position).Unit

Let FastCast handle velocity

Hey! So the issue is actually a parallax problem between your camera and your character. When you use Mouse.Hit.Position as the target, roblox is shooting that ray from the camera’s position, not from your character. so the target point you get is slightly offset to the side depending on where the camera is. then when you compute the direction from HRP to that point, the angle is just slightly wrong and at certain distances or angles it goes completely sideways. thats why it seems random but always happens when something is in your crosshair

the fix is to just cast your own ray from the camera using camera:ViewportPointToRay() with UserInputService:GetMouseLocation(), get the world position it hits, then calculate the direction from your HRP to that point. also make sure you exclude your own character from the raycast params or it’ll just hit yourself and return garbage. once you do that your direction will actually line up with what the camera sees and the shots will go where you aim

1 Like