Help with making a unit movement system similar to HOI4

Hi!

  1. What do you want to achieve? I am trying to make my own grand strategy game, starting with armies.

  2. What is the issue? I cannot think of a good way to make a pathfinding system which will not move along water. I do not want a movement system like Rise of Nations, as I want it to use tiles.

  3. What solutions have you tried so far? I’ve tried making a rudimentary system where the army will detect all touching parts, and which ever one is closer to the destination would be the spot the army moves to, but it would get stuck on coastlines.

ALL SCRIPTS ARE SERVER SIDED

This is my movement script:

local touchingDest = false

local destination = Vector3.new(160.3, 5.25, 66)-- Manual position for testing purposes

local safepos
local previouspos
local nextpos

local tileTouching

local previousDist = (destination - script.Parent.Position).magnitude
local dist

wait(5)

repeat
	previouspos = script.Parent.Position
	tileTouching = workspace:GetPartsInPart(workspace.EU[script.Parent.CurrentTile.Value])
	for _,tile in pairs(tileTouching) do
		if tile.Name ~= "Baseplate" then
			nextpos = tile.Position
			
			dist = math.floor((destination - nextpos).magnitude)
			if dist == previousDist then
				
				script.Parent.Position = nextpos
				previousDist = dist
				safepos = previouspos
				previouspos = nextpos
			elseif dist < previousDist then
				script.Parent.Position = nextpos
				previousDist = dist
				safepos = previouspos
				previouspos = nextpos
			end
		end
	end
	
	if script.Parent.Position == destination then
		touchingDest = true
	end
	
	wait(0.5)
until touchingDest == true

This is the script which colors the tile and tells the movement script what tile it is on:

game:GetService("RunService").Heartbeat:Connect(function()
	local touchingParts = workspace:GetPartsInPart(script.Parent)
	for i,e in pairs(touchingParts) do
		if e.Name ~= "Baseplate" then
			e.Color = script.Parent.Color
			script.Parent.CurrentTile.Value = e.Name
		end
	end
end)

For the above scripts to work, each tile will need to be named differently.

This is my explorer window:

image

Thank you if you are able to help me get this fixed!

1 Like