Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Ray casting pathfinder doesn't work?

I'm trying to make it so the Zombie can detect a player coming from using ray casting but it isn't working! Here is my code.

local ray = Ray.new(script.Parent.Head.Position,(script.Parent.Torso.Position + Vector3.new(50,10,50) - script.Parent.Torso.Position).Unit * 300)
local hit, position = workspace:FindPartOnRay(ray,nil,false,false)
if hit then
	if hit.Parent:FindFirstChild('Humanoid') and hit.Parent:FindFirstChild('Torso') then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			script.Parent.Humanoid:MoveTo(hit.Parent.Torso.Position)
		end
	end
end

1 answer

✓ Accepted answer

You do not need to use raycasting but pathfinding Since I believe raycasting is for bullets and stuff.

local function followPath(destinationObject)
	-- Compute and check the path
	path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.PrimaryPart.Position)
	-- Empty waypoints table after each new path computation
	waypoints = {}
 
	if path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints and start zombie walking
		waypoints = path:GetWaypoints()
		-- Move to first waypoint
		currentWaypointIndex = 1
		humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		-- Error (path not found); stop humanoid
		humanoid:MoveTo(zombie.HumanoidRootPart.Position)
	end
end

1 comment

0

Thanks! I totally forgot about the path finding service!

This discussion is preserved as it was. New answers, comments, and votes are closed.