Raycast ignoring wall blocking

Hello everyone, I placed a wall that very obviously blocks the ray, but the ray doesn’t print anything and just fails.

local ani005335 = game.Workspace:WaitForChild("ani005335")
local HumanoidRootPart = ani005335:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local part = script.Parent
local lookvector = HumanoidRootPart.CFrame.LookVector
local frame = part.Remorse.PrimaryPart
local pos = HumanoidRootPart.Position + HumanoidRootPart.CFrame.LookVector * 20
local bracket = {}
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")



local function VisualizeRay(partPos,direction)
	local distance = (direction-partPos.Position).Magnitude
	local midpoint =  partPos.Position + (direction - partPos.Position) / 2
	local newPart = Instance.new("Part")
	newPart.Anchored = true
	newPart.CanCollide = false
	newPart.Material = Enum.Material.Neon
	newPart.Color = Color3.fromRGB(255,0,0)
	newPart.Size = Vector3.new(0.2,0.2,distance+0.1)
	newPart.CFrame = CFrame.new(midpoint,partPos.Position)
	newPart.Parent = workspace
	game.Debris:AddItem(newPart,0.5)		
end


RunService.Stepped:Connect(function()
--	part:MoveTo(HumanoidRootPart.Position + (HumanoidRootPart.CFrame.LookVector * 100))
--	frame.CFrame = CFrame.new(pos, pos + lookvector)
	local pivot = part:GetPivot()
	local newPivot = CFrame.lookAt(HumanoidRootPart.Position + (HumanoidRootPart.CFrame.LookVector * 30),HumanoidRootPart.Position)
	part:PivotTo(newPivot)
	for i,v in pairs(part.Remorse["Visual Range"]:GetChildren()) do
		local raycastparams1 = RaycastParams.new()
		raycastparams1.FilterType = Enum.RaycastFilterType.Exclude
		raycastparams1.FilterDescendantsInstances = {part,HumanoidRootPart,HumanoidRootPart.Parent.Square}
		local raycast = workspace:Raycast(HumanoidRootPart.Position,v.Position)
		VisualizeRay(HumanoidRootPart,v.Position  )
		if raycast and (raycast.Instance.Name == "Part") == false then
			print(raycast.Distance)
			print(raycast.Instance)
		else
			warn("EAT BUGS")
		end
		
	end
end)

1 Like

You haven’t put your raycast parameters in the raycast btw.

Your problem is the raycasts 2nd parameter should be a direction pointing to where you want the ray to go. You just did .Position

2 Likes

If I remove the .Position, then it will throw an error of unable to cast Instance to Vector3…

1 Like

" the raycasts 2nd parameter should be a direction pointing to where you want the ray to go"

In no way did I imply that you should just remove position.
You need to change it to a vector3 with the direction of where you want it to go and a magnitude of distance.

1 Like

You are visualising the ray, not the raycast…

5 Likes

Try this:

local ani005335 = game.Workspace:WaitForChild("ani005335")
local HumanoidRootPart = ani005335:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local part = script.Parent
local lookvector = HumanoidRootPart.CFrame.LookVector
local frame = part.Remorse.PrimaryPart
local pos = HumanoidRootPart.Position + HumanoidRootPart.CFrame.LookVector * 20
local bracket = {}
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")



local function VisualizeRay(partPos,direction)
	local distance = (direction-partPos.Position).Magnitude
	local midpoint =  partPos.Position + (direction - partPos.Position) / 2
	local newPart = Instance.new("Part")
	newPart.Anchored = true
	newPart.CanCollide = false
	newPart.Material = Enum.Material.Neon
	newPart.Color = Color3.fromRGB(255,0,0)
	newPart.Size = Vector3.new(0.2,0.2,distance+0.1)
	newPart.CFrame = CFrame.new(midpoint,partPos.Position)
	newPart.Parent = workspace
	game.Debris:AddItem(newPart,0.5)		
end


RunService.Stepped:Connect(function()
	--	part:MoveTo(HumanoidRootPart.Position + (HumanoidRootPart.CFrame.LookVector * 100))
	--	frame.CFrame = CFrame.new(pos, pos + lookvector)
	local pivot = part:GetPivot()
	local newPivot = CFrame.lookAt(HumanoidRootPart.Position + (HumanoidRootPart.CFrame.LookVector * 30),HumanoidRootPart.Position)
	part:PivotTo(newPivot)
	for i,v in pairs(part.Remorse["Visual Range"]:GetChildren()) do
		local raycastparams1 = RaycastParams.new()
		raycastparams1.FilterType = Enum.RaycastFilterType.Exclude
		raycastparams1.FilterDescendantsInstances = {part,HumanoidRootPart,HumanoidRootPart.Parent.Square}
		local raycast = workspace:Raycast(HumanoidRootPart.Position,v.Position)
		VisualizeRay(HumanoidRootPart,v.Position  )
		if raycast and raycast.Instance:IsA("Part") then
			VisualizeRay(HumanoidRootPart, raycast.Position)
			print(raycast.Distance)
			print(raycast.Instance)
		else
			VisualizeRay(HumanoidRootPart,v.Position )
			warn("EAT BUGS")
		end

	end
end)

2 Likes

OOOH, you have to specify the distance inbetween the 2 parts for it to work. Thank you so much!!!

--fixed code
local ani005335 = game.Workspace:WaitForChild("ani005335")
local HumanoidRootPart = ani005335:WaitForChild("HumanoidRootPart")
local ani005335 = game.Workspace:WaitForChild("ani005335")
local HumanoidRootPart = ani005335:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local part = script.Parent
local lookvector = HumanoidRootPart.CFrame.LookVector
local frame = part.Remorse.PrimaryPart
local pos = HumanoidRootPart.Position + HumanoidRootPart.CFrame.LookVector * 20
local bracket = {}
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")



local function VisualizeRay(partPos,direction)
	local distance = direction.Magnitude
	local midpoint =  partPos.Position + direction / 2
	local newPart = Instance.new("Part")
	newPart.Anchored = true
	newPart.CanCollide = false
	newPart.Material = Enum.Material.Neon
	newPart.Color = Color3.fromRGB(255,0,0)
	newPart.Size = Vector3.new(0.2,0.2,distance+0.1)
	newPart.CFrame = CFrame.new(midpoint,partPos.Position)
	newPart.Parent = workspace
	game.Debris:AddItem(newPart,0.5)		
end


RunService.Stepped:Connect(function()
	--	part:MoveTo(HumanoidRootPart.Position + (HumanoidRootPart.CFrame.LookVector * 100))
	--	frame.CFrame = CFrame.new(pos, pos + lookvector)
	local pivot = part:GetPivot()
	local newPivot = CFrame.lookAt(HumanoidRootPart.Position + (HumanoidRootPart.CFrame.LookVector * 30),HumanoidRootPart.Position)
	part:PivotTo(newPivot)
	for i,v in pairs(part.Remorse["Visual Range"]:GetChildren()) do
		local raycastparams1 = RaycastParams.new()
		raycastparams1.FilterType = Enum.RaycastFilterType.Exclude
		raycastparams1.FilterDescendantsInstances = {part,HumanoidRootPart,HumanoidRootPart.Parent.Square}
		local raycast = workspace:Raycast(HumanoidRootPart.Position,v.Position-HumanoidRootPart.Position)
--The script above is what I changed
		VisualizeRay(HumanoidRootPart,v.Position-HumanoidRootPart.Position)
		if raycast and (raycast.Instance.Name == "Part") == false then
			print(raycast.Distance)
			print(raycast.Instance)
		else
			warn("EAT BUGS")
		end

	end
end)

I thought it like CFrames, the second parameter is what the ray face towards. It’s actually how far the ray goes from the starting point.

1 Like


big fan of this, keep it up

3 Likes

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