Raycast not working on certain sides of parts

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    code no work

  2. What is the issue? Include screenshots / videos if possible!
    thingamajig
    I’m using raycasting to detect when a wall is infront of my rig, but the raycast only detects the wall on certain sides. (The green rods are where the rig detected a wall)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried moving the start of the raycast to slightly behind the rig and changing the rotation and position of the parts.

local PathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")

local rs = game:GetService("ReplicatedStorage")

local GameEvents = rs:WaitForChild("GameEvents")

local GameEnd = GameEvents:WaitForChild("GameEnd")
local GameStart = GameEvents:WaitForChild("GameStart")

local Zombie = script.Parent
local HRP = Zombie:WaitForChild("HumanoidRootPart")
local ZHum = Zombie:WaitForChild("Humanoid")

local Hidden = Zombie:WaitForChild("!Hidden")

local HumansFolder = workspace.Players.Human

-- Functions
local function LocateHuman()
	local inRange = {}
	local closest = 25
	local target = nil

	if #HumansFolder:GetChildren() < 1 then return end
	
	for _, Human in pairs(HumansFolder:GetChildren())do
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {Human:GetChildren(), Zombie:GetChildren()}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		
		local ray = workspace:Raycast(HRP.Position, Human.HumanoidRootPart.Position, raycastParams)
		--print(Human)
		if ray == nil then
			local magnitude = (HRP.Position - Human.HumanoidRootPart.Position).Magnitude
			--print(magnitude)
			if magnitude <= 25 then
				table.insert(inRange, 
					{Magnitude = magnitude,
						Char = Human
					})
			end
		end
	end
	for _, entry in pairs(inRange) do
		local magnitude = entry.Magnitude
		local char = entry.Char
		
		if magnitude < closest then
			closest = magnitude
			target = char
		end
		--print(magnitude)
		--print(target)
	end
	return target
end

local R2Params = RaycastParams.new()
R2Params.FilterType = Enum.RaycastFilterType.Exclude
task.wait(1)
R2Params.FilterDescendantsInstances = {workspace:WaitForChild("Players")}

local function FrontCheck()
	local obstacle = false
	
	local pos = (HRP.CFrame + HRP.CFrame.LookVector * 3 + Vector3.new(0, -1, 0)).Position
	
	--part.Position = pos + Vector3.new(0, 1.5, 0)
	
	local ray = workspace:Raycast(HRP.Position, pos, R2Params)
	if ray then
		local part = Instance.new("Part", workspace) -- testing purposes
		part.Anchored = true
		part.CanCollide = false
		part.CanQuery = false
		part.Material = Enum.Material.Neon
		part.Color = Color3.new(0.239216, 1, 0.00784314)
		part.Size = Vector3.new(0.2,3,0.2)
		part.Position = ray.Position

		print(ray.Instance)
		print(ray.Instance.Parent)
		ZHum.Jump = true
	end
end

-- Connections
local waiting = false

RunService.Heartbeat:Connect(function()
	local nearestHuman = LocateHuman()
	FrontCheck() --jump
	
	if nearestHuman == nil then return end
	if waiting == true then return end
	if nearestHuman.Humanoid.Health <= 0 then return end

	ZHum:MoveTo(nearestHuman.HumanoidRootPart.Position)
end)
1 Like

Once again, someone still misunderstands what RayDirection parameter does. It’s supposed to be one unit of a direction multiplied by length or distance.

You need to convert pos to a singular unit using Vector3.Unit and then multiply it with a number of choice for how far in front of you.

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