Raycast not working as intended, shoots through bricks

So I have made a gun, roughly; and it works fine
apart
from the fact that it will shoot through objects even with edits.
https://i.gyazo.com/4718f49bd17a6afa00dc5ccc9c6858bd.gif

my local script

local replicatedStorage = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")

local gun = script.Parent
local gunAmmo = gun:GetAttribute("AmmoCount")
local gunAmmoSet = 7
local canReload = true

local fireGunEvent = replicatedStorage.FireGun

local gunDebounce = false
local debounceTime = gun:GetAttribute("Cooldown")

local player = players.LocalPlayer
local mouse = player:GetMouse()

gun.Equipped:Connect(function()
	canReload = true
	
	uis.InputBegan:Connect(function(input, _GameProccessed)
		if _GameProccessed then return end

		if input.KeyCode == Enum.KeyCode.R and canReload == true then
			gunAmmo = gunAmmoSet
			print("reloading!")
		end
	end)
end)

gun.Unequipped:Connect(function()
	canReload = false
	if canReload == false then
		print("gun cannot be reloaded")
	end
end)

gun.Activated:Connect(function()
	if not gunDebounce and gunAmmo >= 0 then
		print(gunAmmo)
		
		gunAmmo -= 1
		
		gunDebounce = true

		local startPos = gun.Barrel.MuzzleFlashAttach.Position
		local endPos = mouse.Hit.Position

		print("gun fired")

		fireGunEvent:FireServer(gun, startPos, endPos)

		task.wait(debounceTime)
		gunDebounce = false
	end
end)

and my server script


local fireGunEvent = replicatedStorage.FireGun

fireGunEvent.OnServerEvent:Connect(function(player, gun, startPos, endPos)
	local MaxDistance = gun:GetAttribute("MaxDistance")
	local Damage = gun:GetAttribute("Damage")
	
	local direction = (endPos - startPos).Unit * MaxDistance
	
	local raycastRes = game.Workspace:Raycast(startPos, endPos - startPos)
	
	
	if raycastRes then
		print(raycastRes)
		
		local hitChar = raycastRes.Instance.Parent
		local humanoid = hitChar:FindFirstChild("Humanoid")
		
		if humanoid then
			if hitChar.Name ~= player.Character.Name then
				humanoid.Health -= Damage
			end
		end
	end
end)

any help?

1 Like
local raycastRes = game.Workspace:Raycast(startPos, direction)

No change, still capable of shooting through walls

maybe bricks don’t have cancollide on?

the said walls have both cancollide true and canquery true so

Make a check to see if it goes through a wall or not then.

how would i go about this? im sorta relying on my current scripts to simply not damage the opposite player if the raycast hits something

I’ll give you that in a second, but why don’t you just use a premade gun system?

I wanted to have the experience of coding my own systems + feels more authentic that way :^)

and also very much appreciated

Alright.

I can agree with that as I’ve made my own systems for stuff like this before.
It is very fun to do.

You can use this post to implement it, but I suggest using something else like FastCast Redux.

I will read into this, thank you !!

This position is not where you think it is, so your ray is not starting where the gun is. Attachment.Position is the translation of the attachment relative to its parent. You probably need this line to change to this:

local startPos = gun.Barrel.MuzzleFlashAttach.WorldPosition

Consider adding some debug visualization. Like you can create a LineHandleAdornment locally, set its Parent and Adornee to game.Workspace.Terrain (convenient because it’s always at 0,0,0) and then set the LineHandleAdornment.CFrame = CFrame.new(startPos, endPos) and LineHandleAdornment.Length = (endPos-startPos).Magnitude. Then, you’ll see where the ray is actually starting and pointing.

2 Likes

I see, Ill do this in a few seconds i am preoccupied but ill let you know :+1:

wowie that was the solution thank you

2 Likes

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