Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by V_R · Dec 05, 2015 at 05:29 PM · vector3coroutineoptimizationcentercalculate

Calculating the Exact Center of Multiple Objects

So I've got this object which 'animals' are supposed to move around and stuff. The problem with this is that I can't get this point to be in the exact center of the GameObjects. If you select multiple objects in the editor, the translation thingy is located in the center of these objects, and that is the point I want, but for some reason, I'm always a couple of units off...


using UnityEngine; using System.Collections; using System.Collections.Generic;

public class groupingObjectController : MonoBehaviour { List animalObjects; List animalTrans = new List(); Vector3 controllerPos = new Vector3(); float count = 0; Vector3 center = new Vector3(0,0,0); Vector3 centroid;

 void Start(){
     StartCoroutine("controllerPosition");
 }

 IEnumerator controllerPosition(){
     while (enabled){
         animalObjects = new List<GameObject>(GameObject.FindGameObjectsWithTag("animal"));
         foreach (GameObject animal in animalObjects){
             center += animal.transform.position;
             count++;
         }
         centroid = center/count;
         //controllerPos = controllerPos/animalTrans.Count;
         transform.position = centroid;
         print ("Yielding");
         yield return true;
     }
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PixelFireXY · Feb 06, 2020 at 02:42 PM 0
Share

Thank you so much!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Feb 06, 2020 at 05:27 PM

So after some experimentation, I believe you are using the "weighted center" whereas unity uses the center of the combined bounding box. Imagine you have 20 objects placed on a line that is 100 units long, and 19 of them are at 0 and 1 is at 100. Your "center" would be close to 0 while Unities "center" would show at 10. If you want to match unity do something like

 private List<Renderer> animalRenderers;
 private List<GameObject> animalObjects;
 
 void Start () {
 
     animalObjects = new List<GameObject> (GameObject.FindGameObjectsWithTag ("animal"));
     animalRenderers = new List<Renderer> ();
     foreach (GameObject animal in animalObjects) {
         animalRenderers.Add (animal.GetComponent<Renderer> ());
     }
     StartCoroutine ("controllerPosition");
 }
 
 IEnumerator controllerPosition () {
     while (enabled) {
 
         Bounds totalBounds = new Bounds ();
         if (animalRenderers.Length > 0) totalBounds = animalRenderers[0].bounds;
 
         foreach (Renderer r in animalRenderers) {
             totalBounds.Encapsulate (r.bounds);
         }
         
         centroid = totalBounds.center;
         //controllerPos = controllerPos/animalTrans.Count;
         transform.position = centroid;
         print ("Yielding");
         yield return true;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by thomp888 · Feb 06, 2020 at 03:05 PM

I think you use 'Bounds'

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Optimization: Central "update" system vs using Updates 1 Answer

Can you suggest a faster way to rotate an object? 1 Answer

How do you calculate the rotation of a hexagon based on the six Vector3 points 0 Answers

How to calculate a Vector3 point having three given Vector3 points and an angle of 90 degrees? 1 Answer

Moving the player toward a direction 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges