How do I make a group of Object tween to the same position without one interfering with another

So I’m making a fish-aquarium type game and this is what i have so far:
640ffae5543e938bb1a173ea7e136ef5

What im trying to do next, is make a swarm of fishes swim together in the same general direction, so I’m wondering how i can do that considering I’m using a specific point for one fish

this is the code im using btw, (in a client sided script):

local fish = game.Workspace.fish
local fishRegionFol = game.Workspace.FishRegion
local region1 = fishRegionFol.Region
local rotateTIme = 0.7

local ts = game:GetService("TweenService")
local tweenInf = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local tweenInf2 = TweenInfo.new(rotateTIme, Enum.EasingStyle.Sine, Enum.EasingDirection.In)

function moveFish()
	local fClone = fish:Clone()
	fClone.Parent = workspace
	spawn(function()
		
		while wait(2) do
			local randX = math.random((region1.Position.X - region1.Size.X/2), (region1.Position.X + region1.Size.X/2))
			local randY = math.random((region1.Position.Y - region1.Size.Y/2), (region1.Position.Y + region1.Size.Y/2))
			local randZ = math.random((region1.Position.z - region1.Size.z/2), (region1.Position.z + region1.Size.z/2))
			
			local randPoint = Vector3.new(randX, randY,randZ)
			
			local lookPoint = CFrame.lookAt(fClone.Position, Vector3.new(randX, randY,randZ))
			
			ts:Create(fClone, tweenInf2, {CFrame = lookPoint}):Play()
			wait(rotateTIme)
			ts:Create(fClone, tweenInf, {Position = randPoint}):Play()

		end
	end)
end

moveFish()

I do have some ideas in mind which may not be the most efficient ex. offsetting one fishes position to the left or right, but wont be very effective when making a large swarm.

I know this has definitely been done before in many games using a lot of math so this is why I’m making this post to ask anyone who knows how to do this efficiently

Yep you are right it’s been done before and it is known as boids.

Just found this video which links a module and the result is really cool:

2 Likes