Raycast Script Activity% - Normal or Abnormal? + Raycast Optimization Tips

How do you do, fellow developers?

I’m making an AI that shoots stuff, and all is great. Optimization is good and I even managed to have 100 instances running around aimlessly.

However - I introduce the very function below (at 60 rays per second for this test) and suddenly one instance can use 1.000% of the script activity.

function makeRaycast()
	local origin = gunMuzzle.Position
	local rayTarget = enemy.Position
	local direction = (rayTarget - origin) + Vector3.new(math.random(spreadMin,spreadMax),math.random(spreadMin,spreadMax),0)

	local result = workspace:Raycast(origin, direction, raycast_params)
	if result then
		local hit = result.Instance
		if hit.Parent:FindFirstChildOfClass("Humanoid") then
			print("raycast hit the humanid!!")
		end
	else
		print('raycast hit nothin')
        --somethin somethin goes here
	end

It’s understandable, that’s alot of rays per second - But something tells me such a function shouldn’t be that heavy and I’m doing something wrong…?

One thing that comes to mind is how I can make parts CanTouch = false or how I can disable Humanoid States and that increases performance. Is there something for Raycasts or is this amount of load just normal for raycasting?

edit: Muting makeRaycast() from the shooting function (making the NPC fire blanks lmao) has the script activity never go above 0.350% so the ‘anomaly’ is definitely here.

ok im just a big idiot this right here is the culprit

obviously - it makes the raycast do extra things even if it didnt hit anything (duh)
script activity down to 0.300 - 400ish after removing that line

Still, optimization tips welcome.

Does it happen if you remove the prints? They will slow things down a lot.