In a tower defence game, how to check which waypoint the zombie is moving to?

function zomb.Move(Zombie)
    local speed = Zombie.cfg.Speed.Value * 100
    local pathway = workspace.pathway
    local BezierPath = require(workspace.BezierPath)
    local waypoints = {pathway.Spawn.Position}
    local offset = math.random(-1,1)
        local movingto = Zombie.waypoint

    for i = 1, #pathway:GetChildren() - 2 do
        table.insert(waypoints, pathway[i].Position)
    end

    table.insert(waypoints, pathway.Exit.Position)

    local NewPath = BezierPath.new(waypoints,4)
    local HumanoidRootPart = Zombie.HumanoidRootPart

    for t = 0,1,1/speed do
        HumanoidRootPart.CFrame = NewPath:CalculateUniformCFrame(t) * CFrame.new(offset, HumanoidRootPart.Size.Y * 1.5, 0)
        task.wait(0.01)
    end
    workspace.map.Base.Humanoid:TakeDamage(Zombie.Humanoid.Health)
    Zombie:Destroy()
end

I’m using BezierPath to move all my Zombies
All zombies are anchored
movingto variable is an int value
I tried using touched event, but it didn’t work for some reason

I will appreciate any help

1 Like

Well two methods:

A: easy method

for i, wp in path:GetWaypoints() do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(.1,.1,.1)
part.Position = wp.Position + Vector3.new(0,2,0)

Now you can just use a touched event on these parts.

I’ll tell you the second method in a sec.

1 Like

Oh, haha, I forgot to say that the waypoints are parts lol

1 Like

Oh well the second method is checking the distances of each waypoint from the NPC, and finding which one is the closest.

1 Like
		for i,v in pairs(pathway:GetChildren()) do
			local distance = (HumanoidRootPart.Position - v.Position).Magnitude
			local closest = nil
			if not closest or distance < closest then
				closest = distance
				print(closest)
			end
		end

I tried the second method and it doesnt work, it prints out all the children inside pathway instead

1 Like

I also wanted to say that using touched method won’t work because both parts are anchored and I use CFrames to move zombies

1 Like

Touched events do work whether the part is anchored or not, what determines if the event is triggered is the cantouch property.

Also why use CFrames to move the zombies? Using :MoveTo() would be much more effective.

The method of lerping CFrames and what not is for models that have alot of baseparts and therefore using regular methods would be laggy. However, a typical zombie NPC would work fine with moveto.

Tons of games use the simple moveto on NPCs and they experience no issues whatsoever, if you want it to be smooth you can mess around with network ownership.

1 Like

I just found using cframes more optimized than moving all the zombies with humanoid:MoveTo() because I plan to make big hordes of zombies and I don’t want big impact on FPS

1 Like

Yes but many games do this even when they have huge hordes of zombies, moveto would be better.

1 Like

here’s the problem, the zombies will slowdown alot and the game will lag awfully, because when i said big hordes of zombies, i meant 300-500 zombies

1 Like

Well I guess that makes sense in some way. Sorry though I’m not sure what the solution is.

2 Likes

I think switching to a Humanoid:MoveTo() approach for moving your NPC would be best. That way, you can define and use the target part elsewhere.

it’s fine, thank you for trying to help solve my problem

1 Like

so here’s what i tried to do

local parts = Zombie["Left Leg"]:GetTouchingParts()
		for _, part in pairs(parts) do
			for _, pathwayPart in pairs(pathway:GetChildren()) do
				if part == pathwayPart then
					Zombie.MovingTo.Value += 1
				end
			end
		end

but i cant get it work

i set value to time to check which zombie is the first so i solved my problem

1 Like

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