How to fix this raycast?

so i recreated the last prism from terraria, but the projectile it makes kind of sucks.
first off, it does the normal raycast hitbox. i TRIED to use shapecasts but it just turned the cube beams to cylinders and they weren’t alligned correctly. i wish i recorded it

second off, the raycast constantly detects where the mouse or part is, so the beams stack on eachother before resetting

flashing lights warning
bad raycast


bad hitbox

here’s the snippets of code where the bugs happen, the part instance (lasers) is meant to be damaging, not the raycast. how do i convert it to a shapecast, and preferably with cylinders?

local script

local folder = game.Workspace.Ignored
-
mouse.TargetFilter = folder
tool.Activated:Connect(function()
	if dbtool == true then
		return end
	dbtool = true
	dbtrigger = true
	
	while dbtrigger == true do
		--print(tool:GetChildren())
		local begin = tool.Start.Position
		local finish = mouse.Hit.Position

		remote:FireServer(tool, begin, finish)
		task.wait(cooldown)
	end
	dbtool = false
end)

server script

local direction = (finish - begin).Unit * range
	local detect = game.Workspace:Raycast(begin, direction)
	if detect and not prismFunction(begin, direction) then
		print(detect.Instance.Name)
		local mark = detect.Instance.Parent
		local core =  mark:FindFirstChild("Humanoid")

		if core then
			if mark.Name ~= player.Character.Name then
				if detect.Instance.Name == "Head" then
					core.Health -= critical
				else
					core.Health -= damage
				end
			end
		end
	end

	prismFunction(begin, direction)
end)
1 Like

you can use raycastparams and collision groups to allow the raycast to ignore the beam parts

1 Like

you should use raycast params which can make your raycast ignore certain instances
here’s the docs for it

you can use this in ur case like so:

local direction = (finish - begin).Unit * range

local folder = ... -- make this a folder where u place all the parts u want to ignore here
local params = RaycastParams.new()
params.FilterDescendantsInstances = folder:GetChildren()
params.FilterType = Enum.RaycastFilterType.Exclude -- this means to ignore all objects inside the folder

local detect = game.Workspace:Raycast(begin, direction, params) -- we add params to the raycast args
if detect and not prismFunction(begin, direction) then
	print(detect.Instance.Name)
	local mark = detect.Instance.Parent
	local core =  mark:FindFirstChild("Humanoid")

	if core then
		if mark.Name ~= player.Character.Name then
			if detect.Instance.Name == "Head" then
				core.Health -= critical
			else
				core.Health -= damage
			end
		end
	end
end

prismFunction(begin, direction)
end)

in order to add the laser beams ur making inside the folder you just parent them to the folder when u create them like so:

local laser = ....:Clone() -- or watever the way ur making it
laser.Parent = folder -- make sure its the same folder used in the raycast params
1 Like

thanks for the docs, can’t believe it was easy to fix lol, what about shapecasting?

yeah its not that hard,
shapecasting is basically raycasting but its more custumizable
you can cast rays in different shapes, sizes.
there’s currently 2 types os shapecasting:
Blockcast, Spherecast

WorldRoot:Blockcast(
    cf: CFrame,
    size: Vector3,
    direction: Vector3,
    params: RaycastParams?
): RaycastResult?

Documentation for Blockcast

WorldRoot:Spherecast(
    pos: Vector3,
    radius: number,
    direction: Vector3,
    params: RaycastParams?
): RaycastResult?

Documentation for Spherecast

if you found my raycast params answer helpful I would appreciate it if you solution it :smile:


same thing, i think even worse now, what gives?

you’re probably not adding the laser parts to the folder can you show me where u make the lasers spawn?

1 Like

hi sorry for the late reply
server script

local tool = game.StarterPack.Apotheosis
local laser = replicate.prismRemote.Laser_PrismRemote

local function prismFunction(begin, direction)
	local center = begin + direction / 2
	local distance = direction.Magnitude
	local random = Random.new()
	local folder = game.Workspace.Ignored

	local projectile = Instance.new("Part")
	projectile.Parent = folder
	projectile.Anchored = true
	projectile.CanCollide = false
	projectile.Material = Enum.Material.Neon
	projectile.Color = Color3.fromRGB(math.random(0, 255), math.random(0, 255), math.random(0, 255))
	projectile.CFrame = CFrame.new(center, begin)
	projectile.Size = Vector3.new(3, 3, distance)
	local fade = tweens:Create(projectile, TweenInfo.new(time), {Transparency = 1}):Play()
	debris:AddItem(projectile, .1)
end

laser.OnServerEvent:Connect(function(player, tool, begin, finish)
	local damage = tool:GetAttribute("Damage")
	local critical = tool:GetAttribute("Critical")
	local range = tool:GetAttribute("Range")
	tool.shot:Play()
	local direction = (finish - begin).Unit * range
	
	local folder = game.Workspace.Ignored
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = folder:GetChildren()
	params.FilterType = Enum.RaycastFilterType.Exclude
	
	local detect = game.Workspace:Raycast(begin, direction, params)
	if detect and not prismFunction(begin, direction, params) then
		print(detect.Instance.Name)
		local mark = detect.Instance.Parent
		local core =  mark:FindFirstChild("Humanoid")

		if core then
			if mark.Name ~= player.Character.Name then
				if detect.Instance.Name == "Head" then
					core.Health -= critical
				else
					core.Health -= damage
				end
			end
		end
	end

	prismFunction(begin, direction)
end)

can u try this, i forgot to add {}

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {folder:GetChildren()}
	params.FilterType = Enum.RaycastFilterType.Exclude
1 Like

Try switching the CanQuery attribute of the projectiles to false.

1 Like

this was also the solution but i could only mark one, thank you!!!

1 Like

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