(yt video bc apparently i can’t upload a video that’s over 0.001 seconds)
origin = muzzle.Position
dir = muzzle.CFrame.LookVector * currentWeaponData.MaxDist
--(muzzle is the tip of the gun which is rendered only on the client. in this case, maxdist is 500)
function module.GunToMouseCast(origin,dir,data,list,aiming)
local spreadSub = 0
if aiming then
spreadSub = data.AimSpreadDecrease
end
local spreadX = math.random((-data.Spread+spreadSub)*100,(data.Spread-spreadSub)*100)/100
local spreadY = math.random((-data.Spread+spreadSub)*100,(data.Spread-spreadSub)*100)/100
local spreadZ = math.random((-data.Spread+spreadSub)*100,(data.Spread-spreadSub)*100)/100
local fullSpread = Vector3.new(spreadX,spreadY,spreadZ)
--i've already concluded that the spread isn't the issue b/c it still happens when i remove it. it's the fault of the direction
dir += fullSpread
local part,pos,norm = workspace:FindPartOnRayWithWhitelist(Ray.new(origin,((dir-origin).Unit*data.MaxDist)),list)
return part,pos,norm
end
also i’m aware that my form of raycasting is deprecated. i felt the need to clarify that b/c someone has pointed that out rather than helping me in the past
the origin is fine and works as it’s supposed to, it’s the direction that’s messed up. if ur wondering anyway, though, it always prints the position of the muzzle (aka what the origin actually is)
Could you print the muzzle LookVector and the camera’s LookVector alongside? I know they’re not the same but I wanna see something about the first LookVector
Perhaps you have calculated the direction incorrectly? Maybe the gun shoots in different directions since the camera does not appear to be centered relative to the muzzle, causing the ray to point elsewhere.
Otherwise, replace the second parameter in Ray.new() with the new calculated direction as follows:
local mouse = game:GetService("Players").LocalPlayer:GetMouse
local dir = (origin-mouse.Hit.Position).Unit
I recommend using Random.new() instead of math.random() for bullet spread, but that is entirely your decision.
If all else fails, instead of (dir-origin).Unit*data.MaxDist, try doing dir.Unit as the second parameter in Ray.new()
I’m going to state the obvious, which you’ve already been told, but using the deprecated one can and will cause these issues… I had it myself when doing something similar. This should work for your use-case, BUT you’ll have to mess with the direction as LookVector is relative to the part, so either moving the part to be aligned, using a different direction, or offsetting it… purely your decision.
local rp = RaycastParams.new()
rp.FilterDescendantsInstances = { tool.Parent }
rp.FilterType = Enum.RaycastFilterType.Exclude
rp.RespectCanCollide = true
local origin = tool.Handle.CFrame.Position
local direction = handle.CFrame.LookVector * 30
local result = workspace:Raycast(origin, direction, rp)
--If the ray doesnt hit anything it becomes nil, this way we get a default
local endPosition = result and result.Position or (origin + direction)
local beam = Instance.new("Part")
beam.Parent = workspace
beam.BrickColor = BrickColor.new("New Yeller")
beam.Material = Enum.Material.Neon
beam.Transparency = 0.8
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (origin - endPosition).Magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(origin, endPosition) * CFrame.new(0, 0, -distance / 2)
dir.unit as the second parameter just causes it to shoot to the middle of the map. also i’m trying to get it to start at the muzzle and in the muzzles lookvector, not the mouse
ok so i realized that i’m literally using a LOOKVECTOR, not a position, meaning that there was no reason for me to calculate it like:
local part,pos,norm = workspace:FindPartOnRayWithWhitelist(Ray.new(origin,((dir-origin).Unit*data.MaxDist)+fullSpread),list)
so doing it like:
local part,pos,norm = workspace:FindPartOnRayWithWhitelist(Ray.new(origin,dir+fullSpread),list)
fixed it. the reason it was like the other way in the first place was b/c i used to use the mouse position rather than the lookvector of the muzzle, and i forgot to change it after switching.
My mistake, Ray.new() takes in the actual direction instead of the unit direction. Never used Ray.new() or read any of its documentation, should have worked the same with workspace:Raycast().