Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Max = 200
- local start = game.Workspace.Flock.Part
- local pos,size = game.Workspace.Area:GetBoundingBox()
- local RunService = game:GetService("RunService")
- local physics = game:GetService("PhysicsService")
- local modelgroup = physics:CreateCollisionGroup('p')
- physics:CollisionGroupSetCollidable('p','p',false)
- pos = pos * Vector3.new() -- turning it into a vector3 value
- size = size/2
- local function Rand()
- return Vector3.new(math.random(-size.X,size.X),math.random(-size.Y,size.Y),math.random(-size.Z,size.Z))
- end
- local parts = {}
- physics:SetPartCollisionGroup(game.Workspace.Baseplate,'p')
- table.insert(parts,start)
- for i = 1,Max do
- local clone = start:Clone()
- clone.Position = pos + Rand()
- clone.CanCollide = true
- clone.Massless = true
- clone.CFrame = clone.CFrame * CFrame.Angles(0,math.rad(90),0)
- clone.Parent = workspace.Flock
- physics:SetPartCollisionGroup(clone,'p')
- table.insert(parts,clone)
- end
- local Speed = 20
- for i = 1,#parts do
- local v = parts[i]
- local force = Instance.new("BodyVelocity")
- force.MaxForce = Vector3.new(5e4,5e4,5e4)
- force.Velocity = Vector3.new(0,0,0)
- force.P = 1000 * Speed
- local gryo = Instance.new("BodyGyro")
- gryo.MaxTorque = Vector3.new(5e4,5e4,5e4)
- gryo.Parent = v
- force.Parent = v
- end
- local MaxDist = 5
- local SeperationPriority = 2e8
- local CohestionProprity = 2
- local AllignmentProprity = 2
- function Seperationmth(vector)
- return vector * SeperationPriority
- end
- function AverageDistancemath(vector,num)
- return (vector / num) * CohestionProprity
- end
- function Allign(vector)
- return (vector).Unit * AllignmentProprity
- end
- local function StartMovement()
- for Boid = 1,#parts do
- local v = parts[Boid]
- local force = v.BodyVelocity
- local gyro = v.BodyGyro
- local num = 0
- local seperation = Vector3.new()
- local avgDirection = Vector3.new()
- local Allignment = Vector3.new()
- for i = 1,#parts do
- local other = parts[i]
- local mag = (v.Position - other.Position).Magnitude
- if other ~= v and mag < MaxDist then
- num = num + 1
- seperation = seperation + ((v.Position-other.Position) * mag)
- avgDirection = avgDirection + other.Position
- Allignment = Allignment + other.Velocity
- end
- end
- force.Velocity = v.CFrame.LookVector * 8
- local NewPos = CFrame.new(v.Position,num ~= 0 and Seperationmth(seperation) + AverageDistancemath(avgDirection,num) + Allign(Allignment) or pos)
- gyro.CFrame = NewPos
- end
- end
- RunService.Heartbeat:Connect(StartMovement)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement