Raycast ignoring collision, need alternative method

I have a spinning target circle that’s supposed to represent where to aim. The only problem is that the raycast is ignoring collision, and I don’t know an alternative to raycasting.

The spinning circle is at the end of the beam. You can see it no-clipping through the blocks.
https://gyazo.com/949672323bddc4e4f17d7bc67b213c30

local RepStorage = game:GetService('ReplicatedStorage')
--local SwitchSling = RepStorage:WaitForChild('Remotes'):WaitForChild('SwitchSling')
local player = game:GetService('Players').LocalPlayer
local character = player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild('HumanoidRootPart')
local Mouse = player:GetMouse()
local camera = workspace.CurrentCamera

local Cursor = RepStorage:WaitForChild('Models'):WaitForChild('Cursor')
local WorkCursor = workspace:WaitForChild('Cursor')
local Arrow = Cursor:WaitForChild('Arrow')
local Circle = Cursor:WaitForChild('Circle')
local Line = Cursor:WaitForChild('Line')
local LineLine = Line:WaitForChild('Line')
local Line2 = Line:WaitForChild('Line2')

local MOUSE_DOWN = false

local MOUSE_X
local MOUSE_Z

--[[SwitchSling.OnClientEvent:Connect(function()
	MOUSE_X = nil
	MOUSE_Z = nil
end)]]

Line.Parent = WorkCursor
Arrow.Parent = WorkCursor
Circle.Parent = WorkCursor

task.spawn(function()
	while task.wait() do
		for i = 1, 180 do
			task.wait()
			Circle.Rotation = Vector3.new(0, i, 0)
		end
	end
end)

Mouse.Move:Connect(function()
	if MOUSE_DOWN then
		MOUSE_X = Mouse.Hit.Position.X
		MOUSE_Z = Mouse.Hit.Position.Z
	end
end)

Mouse.Button1Down:Connect(function()
	MOUSE_DOWN = true
end)

Mouse.Button1Up:Connect(function()
	MOUSE_DOWN = false
end)

while task.wait() do
	local pos = Vector3.new(HumanoidRootPart.Position.X, 17, HumanoidRootPart.Position.Z - 2)
	if MOUSE_X and MOUSE_Z then
		Line:PivotTo(CFrame.lookAt(pos, Vector3.new(MOUSE_X, 17, MOUSE_Z)) * CFrame.fromEulerAnglesXYZ(0, math.rad(180), 0))
	else
		Line:PivotTo(CFrame.new(pos) * CFrame.fromEulerAnglesXYZ(0, math.rad(180), 0))
	end
	Arrow.CFrame = Line2.CFrame * CFrame.new(0, Line2.Size.y/2 - .28, Line2.Size.z/2) * CFrame.fromEulerAnglesXYZ(0, math.rad(180), 0)

	local raycastparams = RaycastParams.new()
	raycastparams.FilterType = Enum.RaycastFilterType.Exclude
	raycastparams.FilterDescendantsInstances = {character, Arrow, Line, Circle, game:GetService('CollectionService'):GetTagged('ShootingZone'), game:GetService('CollectionService'):GetTagged('Barrier')}
	local result = workspace:Raycast(Line2.Position, Line2.CFrame.LookVector * -100, raycastparams) -- Raycast 10 studs forward

	if result and result.Instance.Parent then
		if result.Instance:IsA('BasePart') then
			local Inst = result.Instance
			if game:GetService('CollectionService'):HasTag(Inst.Parent, 'Block') or Inst.Parent.Name == 'LeftWall' or Inst.Parent.Name == 'MiddleWall' or Inst.Parent.Name == 'RightWall' then
				Circle.Position = result.Position
				Line2.Size = Vector3.new(0.5, 0.5, math.abs(Circle.Position.Z - LineLine.Position.Z)/2)
				Line2.CFrame = Line.PrimaryPart.CFrame * CFrame.new(0, 0, Line2.Size.Z / 2)
			end
		end
	end
end

you’re setting the circle’s position to match the raycast result. raycasts are straight lines and the ‘position’ property only considers the object’s center. to keep the circle from passing through blocks, u should use spherecasting instead.

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