Why is the time to raycast different in this scenario?

Hi everyone,

I’ve finally finished doing the basic things for my NPC framework. But, as I was benchmarking the time, I noticed that in one place, raycasting took longer than the other times it raycasted.

Here’s the script for the raycasting if it helps.

function zombie:attack(tar, dt)
	if self.follow then return end
	local root_pos = self.root.Position
	local root_dir = self.root.CFrame.LookVector
	local from_root = (tar.Position - root_pos).Unit

	local result = workspace:Raycast(root_pos, from_root * 10, self.ignore)
	if result then
		local hit = result.Instance
		local possible = hit:FindFirstAncestorOfClass("Model")
		local npc = hit.Parent:FindFirstChild("bot")
		local plr_char = PA:GetPlayerFromCharacter(possible)
		local is_part = hit:IsA("BasePart")and hit.Name ~= "HumanoidRootPart" or hit:IsA("UnionOperation")
		if plr_char then
			-- damage!!!!!
		elseif is_part and not npc then 
			return true -- follow the path
	self.root.CFrame += from_root * (MOVE_CF_RATIO * 60 * dt)

I’m not sure what I did wrong here or anywhere, so why is the raycast taking longer in some spots than in others? Please leave suggestions below and thanks for your time!

Looks ok, it could just be the pc lag due to other cpu stuff going in the background or even other scripts. Usually this is why we use averages during benchmarking from what I know.

However, try dumping it according to this microprofiler tip and analyzing it according to documentation.