Finding the center point of a group of game objects

Hi there. Im trying to find the central point of a group of game objects to include cohesion in a flocking sim. Im using this code to find the local center, but an error keeps coming up saying “bad argument #1 to ‘pairs’ (table expected, got nil)” even though it worked in anther part of my program. Does anyone have any ideas? : )

Ok so i fixed the error but im being returned with vmath.vector3(nan, nan, nan) and im not sure why?

Looks like you’re iterating through the keys in your table - should it be the values instead?

for _, sheepId in pairs(self.sheep) do

Otherwise check the value of position before adding it to the total.

2 Likes