Why is the shooting weird near walls?


And this is when im near a wall:

Code for the raycast:

local loop
	
	local Bullet = Instance.new("Part")
	Bullet.Size = Vector3.new(0.1, 0.1, 5)
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.Color = Color3.fromRGB(219, 239, 0)
	Bullet.Material = Enum.Material.Neon
	Bullet.Parent = game.Workspace
	
	Bullet.CFrame = CFrame.new(origin, endposition)
	loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
	Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
		
	local hit = workspace:Raycast(Bullet.Position, Bullet.CFrame.LookVector * velocity * 1.5)
		--if (Bullet.Position - origin).Magnitude > 5000 then
		if hit then
			print("HIT: "..hit.Instance.Name)
				if damage ~= nil then
					game.ReplicatedStorage.Events.Damage:FireServer(hit.Instance, 10)
				else
					loop:Disconnect()
					Bullet:Destroy()
				--end
			end
		end
	end)

I don’t know if the bug is in this code tho, but seems like it.

the origin is:

local castParams = RaycastParams.new()
	castParams.IgnoreWater = true
	castParams.FilterType = Enum.RaycastFilterType.Exclude
	castParams.FilterDescendantsInstances = {viewmodel, game.Players.LocalPlayer.Character}

	local Mouse = module.GetMouse(1000, castParams)

and the getmouse function is:

function module.GetMouse(Distance, CastParams)
	local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
	local UnitRay = game:GetService("Workspace").Camera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y)
	
	local origin = UnitRay.Origin
	local endp = UnitRay.Direction * Distance
	local Hit = game:GetService("Workspace"):Raycast(origin, endp, CastParams)
	
	if Hit then
		return Hit.Position
	else
		return UnitRay.Origin + UnitRay.Direction * Distance
	end
end

1 Like

some physics thing causing it to reflect off (Sorry i don’t know a solution but that’s all what i can say D:

Bullet.CanCollide = false

oh yeah try that but i don’t know if it will work when damaging a humanoid

No i mean i had CanCollide disabled for the bullets to work right but that doesnt affect the weird bounce effect or whatever

You are making your bullet face towards wherever the mouse hit. If your mouse happens to hit behind to the barrel of your gun then it’s just going to make your bullet face backwards.

I’d recommend just taking the direction of your camera (assuming you are in first person) and applying that on top.

Bullet.CFrame = CFrame.new(origin, origin + Camera.CFrame.LookVector)
1 Like

That worked but the bug included it just going trough walls
For example glass:


when im near, it just goes trough, when im far it works fine. I have 0 clue how to fix this

No, the gun wasn’t even clipping trough the glass even 1%

Your bullet is just spawning too far forward, I think fastcast also has this same issue (as they handle projectiles practially the same as you are right now).

The easiest solution would probably just be to draw a raycast between where your bullet spawns and the barrel of your gun.

local FilterSelf = RaycastParams.new()
FilterSelf.FilterType = Enum.RaycastFilterType.Exclude
FilterSelf.FilterDescendantsInstances = {Character}

--do this *before* you spawn your bullet

--instead of barrel origin you can also (maybe?) use something a bit further back just in case you stick your barrel inside of a wall
--doing that would also probably stop any weird shooting through walls shenanigans
local PrefireCast = workspace:Raycast(BarrelOrigin, BulletOrigin-BarrelOrigin, FilterSelf)

if PrefireCast then
   --we hit an object, dont spawn a bullet and immediately skip to the hit routine
end

Used this methood a bit, but changed it up to work with any gun without needing to make extra components, and this is alot safer:

local PrefireCast = workspace:Raycast(game.Players.LocalPlayer.Character["HumanoidRootPart"].Position, weapon.GunComponents.BulletSpawn.Position-game.Players.LocalPlayer.Character["HumanoidRootPart"].Position, params)

Checking from the HRP.

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