21 de dez. de 2017

Using Bounds Class to get center of many objects

We can use the Bounds Class to get the center of many objects in the scene.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 private Vector3 GetCenter()
 {
  if (targets.Count == 1)
   return targets[0].position;

  Bounds bound = new Bounds(transform.position, Vector3.zero);
  
  foreach (Transform target in targets)
  {
   bound.Encapsulate(target.position);
  }

  return bound.center;
 }

Nenhum comentário: