How do I make multiple objects move to a single target without pushing each other?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Im looking for a system that makes multiple object to not push each other while moving to a single target.

  2. What is the issue? Include screenshots / videos if possible!
    I’ve been working on a RTS game, based on other games like Medieval RTS and Normal Real Time Strategy Game, these two games have a group movement system, this means that multiple units will be able to move at the same time without selecting them individually. The issue is that I couldn’t find any way to individually modify the target position from every selected unit based on the individual unit current position.

    *Note: A “Target” is basically where the player clicks (This works using Mouse.Hit)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried many different ways, but I couldn’t find any solution to this.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

My code:

MoveUnitEvent.OnServerEvent:Connect(function(player,hitpos,selected)
	local loop = true
	local vector = Vector3.new(selected.Parent.HumanoidRootPart.Position.X/selected.Parent.HumanoidRootPart.Position.X*selected.Parent.HumanoidRootPart.Position.X/distance,0,selected.Parent.HumanoidRootPart.Position.Z/selected.Parent.HumanoidRootPart.Position.Z*selected.Parent.HumanoidRootPart.Position.Z/distance)
	
	local FixedHitpos = hitpos + vector -- IMPORTANT: Here is the issue
	print(vector)

	positions[selected] = {FixedHitpos}
	selected.Parent.HumanoidRootPart.BodyGyro.CFrame = CFrame.lookAt(selected.Parent.HumanoidRootPart.Position,hitpos)
	
	game.ReplicatedStorage.RemoteEvents.CreateBeam:FireClient(player,positions[selected][1],selected.Parent,false)

	--// Start a loop that constantly changes PlaneVelocity according to the position of Unit
	while loop and selected.Parent and positions[selected][1] == FixedHitpos do
		selected.Parent.HumanoidRootPart.Vel.PlaneVelocity = Vector2.new(FixedHitpos.X - selected.Parent.HumanoidRootPart.Position.X,FixedHitpos.Z - selected.Parent.HumanoidRootPart.Position.Z).Unit * selected.Parent.Settings.Stats.Speed.Value
		

		if (selected.Parent.HumanoidRootPart.Position - positions[selected][1]).Magnitude > 1 and selected.Parent and positions[selected][1] == FixedHitpos then
			selected:SetAttribute("repeating",true)
		else
			loop = false
			selected:SetAttribute("repeating",false)
		end
		task.wait(.1)
		
		if selected.Parent then
			if selected:GetAttribute("repeating") == false then
				print("stopped")
				selected.Parent.HumanoidRootPart.Vel.PlaneVelocity = Vector2.new(0,0)
				game.ReplicatedStorage.RemoteEvents.CreateBeam:FireClient(player,positions[selected][1],selected.Parent,true)
			end
		end
	end

		
	
end)

If I wasn’t clear, feel free to reply!

(This is my very first topic)

2 Likes

You could do that by defining an offset for each individual object from the middle point of all of the objects (perhaps the average vector). then just add that offset to the target position to find the position of the unit / object.

local AveragePosition
local UnitPosition

local UnitOffset = UnitPosition - AveragePosition

local TargetPosition
local UnitTargetPosition = TargetPosition + UnitOffset
2 Likes

Thanks for replying, I would like to know what “AveragePosition” is, since Im not really sure how do I get the middle point of all the objects.

Edit: Im not sure how do I get the “average vector”, but for the rest i got it clear

1 Like

The way you would do that is just adding up all the unit vectors and dividing by the unit amount

local AveragePosition 

for _, unit in Units do
   AveragePosition += unit.Position
end

AveragePosition /= #Units
1 Like

Thanks, im already implementing this to my game, I’ll update this reply when I get the results.

Edit: Seems to work but Units arent moving to the correct direction, I will reply after recording a video to show it

2000 years later: Yes, it worked really well, my apologies for not “updating” lol

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