Hello! I’m trying to make a bullet system similar to Gungeon or Nuclear Throne, but my bullets aren’t acting as intended.
My code makes a ray from attachments in the player’s root part to the mouse’s position on the screen, but I need a way to only get the way the mouse is facing, not to point directly at the mouse.
These are the snippets of code that handle the direction, I’m not the best at math so any help would be appreciated, thanks!
local function planeRay(rp, rd, pp, pn)
local scalar = (pp - rp):Dot(pn) / rd:Dot(pn)
return rp + rd * scalar
end
local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)
for point, amt in pairs(config.BAmt) do
local dir = planeRay(unitRay.Origin, unitRay.Direction, firePoints[point].WorldPosition, Vector3.new(0, 1, 0))
events.FB:FireServer(config.BType, config.BAmt, firePoints[point].WorldPosition, CFrame.new(firePoints[point].WorldPosition, dir).LookVector, 135, filter, debounce)
end
You would most likely have to raycast the ray relative to the look vector of where the gun is facing itself in order to calculate its direction, then multiply by a scalar. Maybe manipulate one of the guns to face the same direction but clamp the distance by creating some offset on both sides of the cursor (Not sure whether the rays being parallel has to do with a shotgun mechanism minus the bullet spread)
I took this idea and instead of moving the guns and using their direction, made a part that follows the root part of the player and turns to face the mouse instantly, this way I could use the direction of the attachments in this part to get the bullets flying the right way.