Gun just shoots at a completely different area when i move spots

(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

2 Likes

What’s the output of print(origin) everytime you try to shoot? Move around and do a few shots

Forget that, is this script on the server or client?

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)

everything about the viewmodel is completely on the client. the only time the direction is ever calculated is on the client.

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

(muzzle/origin)

-0.935093343257904, -0.05916552245616913, -0.3494279682636261 -0.928931713104248, -0.06081567704677582, -0.36522233486175537

(camera)

-748.0746459960938, -47.33241653442383, -279.5423889160156 

Yeah, I don’t know, what’s the output when you’re looking straight down, straight up and straight forward?

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)

this didn’t fix it. nothing really changed at least from what i could tell

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().

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.