So, im making a rts game, and my current problem is making CUSTOM unit movement path, not just to one point, but going to one after another
I want to achieve something similar to normal real time strategy game
(when you hold Left CTRL, you can make multiple walkpoints for your units to move them better)
I’ve tried to do something, and somehow it came out mirrored (and overall the scripting is terrible, im sorry) so ask any questions if you don’t understand what’s going on in there
local script, that fires the event to the server:
elseif inp.UserInputType == Enum.UserInputType.MouseButton1 and Selected.Visible == true then
if unit:IsA("Model") then
local mouseLoc = uis:GetMouseLocation()
local ray = camera:ViewportPointToRay(mouseLoc.X,mouseLoc.Y)
local raycast = workspace:Raycast(ray.Origin,ray.Direction * 500)
if raycast then
local multiMove = false
if uis:IsKeyDown(Enum.KeyCode.LeftControl) then
multiMove = true
else
multiMove = false
end
print(multiMove)
local args = {unit,raycast.Position,player,multiMove} -- unit,pos,player and is it multi-spawn(true) or one-time movement(false)
remote:FireServer("MoveTo",args)
end
end
server script, that calls a module script function:
rs.GameControl.OnServerEvent:Connect(function(player,command,args)
local connection
connection = GameController[command](args)
if connection then
connection:Disconnect()
end
end)
function in the module script:
function GameController.MoveTo(args)
local connect
if args[4] == true then
count += 1
else
count = 1
end
print(count)
local unit = args[1]
for i,v in pairs(unit:GetChildren()) do
if v:IsA("Part") then
v:SetNetworkOwner(args[3])
end
end
local position = args[2]
positions[unit] = {}
positions[unit][count] = position
for i,pos in ipairs(positions[unit]) do
print("i = "..i)
connect = Move(unit,position)
IsMoving = true
repeat
task.wait(.5)
until IsMoving == false
unit.PrimaryPart.Vel.PlaneVelocity = Vector2.new(0,0)
if connect then
connect:Disconnect()
end
end
table.clear(positions[unit])
end
and Move() function, that module script uses (based of that medieval rts creator’s tutorial)
local positions = {}
local count = 0
local IsMoving = false
local function Move(unit,position)
print(position)
while unit and unit.Parent do
unit.Humanoid.AutoRotate = false
unit.PrimaryPart:FindFirstChild("Vel").PlaneVelocity = Vector2.new(position.X - unit.PrimaryPart.Position.X,position.Z - unit.PrimaryPart.Position.Z).Unit * 5
unit.PrimaryPart:FindFirstChild("Rot").CFrame = CFrame.lookAt(unit.PrimaryPart.Position,Vector3.new(position.X,unit.PrimaryPart.Position.Y,position.Z))
if (unit.PrimaryPart.Position - position).Magnitude <= 1 then
IsMoving = false
unit.PrimaryPart:FindFirstChild("Vel").PlaneVelocity = Vector2.new()
break
end
task.wait(0.05)
end
end