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
0
Question by bsmx125 · Jun 05, 2019 at 03:56 PM · physicscollidercollidersmathsimulation

Make sphere collider the center of gameobjects/Sum of vectors

Hi, I have problems setting the center of a sphere collider at the center of spawned gameobjects, it moves but not to the center of the gameobjects.

edit: I've found that the sum of Vectors doesn't give the correct number, thats the problem, but i don't know why it happens

This is the code:

 void SphereRadious()
     {
         //4/3 * pi * r^3 = V ; Vt =  V * (protons + neutrons) ; r^3 = Vt/(4/3 * pi) ; r = sqrt(Vt/(4/3 * pi))
         float atomVol = 4 / 3 * Mathf.PI * 1;
         float totalVol = atomVol * (protonsAmount + neutronsAmount);
         float r = Mathf.Pow(totalVol / ((4 / 3) * Mathf.PI), 1f/3f);
         sphereCollider.radius = r;
         radiousOfSpawn = r;

         //PROBLEM:

         float x = 0;
         float y = 0;
         float z = 0;
 
 
         for (int p = 0; p < protonsAmount; p++)
         {
             for(int n = 0; n < neutronsAmount; n++)
             {
                 x = 0; y = 0; z = 0;
                 x += (protons[p].transform.localPosition.x + neutrons[n].transform.localPosition.x);
                 y += (protons[p].transform.localPosition.y + neutrons[n].transform.localPosition.y);
                 z += (protons[p].transform.localPosition.z + neutrons[n].transform.localPosition.z);
                 sphereCollider.center = new Vector3(x, y, z);
             }
         }
     }




alt text

edit2: i've also tried this but the same result:

         float x = 0;
         float y = 0;
         float z = 0;
         
 
         for (int p = 0; p < protonsAmount; p++)
         {
             float xp = protons[p].transform.localPosition.x;
             float yp = protons[p].transform.localPosition.y;
             float zp = protons[p].transform.localPosition.z;
             x += xp;
             y += yp;
             z += zp;
         }
 
         for (int n = 0; n < neutronsAmount; n++)
         {
             float xn = neutrons[n].transform.localPosition.x;
             float yn =  neutrons[n].transform.localPosition.y;
             float  zn = neutrons[n].transform.localPosition.z;
             x += xn;
             y += yn;
             z += zn;
         }
         sphereCollider.center = new Vector3(x, y, z);
capture.png (65.3 kB)
Comment
Add comment · Show 4
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 mikedg1 · Jun 05, 2019 at 12:19 PM 0
Share

Can you try adding a child game object of a small Cube to your game object, with local position of 0,0,0? Then post a picture of your game object selected?

avatar image bsmx125 mikedg1 · Jun 05, 2019 at 12:33 PM 0
Share

To what gameObject? to the spawned gameObject?

avatar image mikedg1 bsmx125 · Jun 05, 2019 at 12:51 PM 0
Share

yes, the spawned game object that you want to center the collider on

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bsmx125 · Jun 06, 2019 at 12:33 PM

 float x = 0;
 float y = 0;
 float z = 0;
   
 for (int p = 0; p < protonsAmount; p++)
 {
     //x = 0; y = 0; z = 0;
     float xp = protons[p].transform.localPosition.x;
     float yp = protons[p].transform.localPosition.y;
     float zp = protons[p].transform.localPosition.z;
     x += xp; y += yp; z += zp;
 }

 for (int n = 0; n < neutronsAmount; n++)
 {
     float xn = neutrons[n].transform.localPosition.x;
     float yn =  neutrons[n].transform.localPosition.y;
     float  zn = neutrons[n].transform.localPosition.z;
     x += xn; y += yn; z += zn;
 }
 float totalPar = neutronsAmount + protonsAmount;
 x /= totalPar; y /= totalPar; z /= totalPar;

 sphereCollider.center = new Vector3(x, y, z);
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 Sazails · Jun 05, 2019 at 04:31 PM

Hopefully this is what you mean?

     public GameObject[] GOs;
     private float totalX = 0f;
     private float totalY = 0f;
     private float totalZ = 0f;
 
     public Vector3 center; // This is the center of all the objects 
 
     void Start()
     {
         foreach (GameObject GO in GOs)
         {
             totalX += GO.transform.position.x;
             totalY += GO.transform.position.y;
             totalZ += GO.transform.position.z;
         }
         center.x = totalX / GOs.Length - 1;
         center.y = totalY / GOs.Length - 1;
         center.z = totalZ / GOs.Length - 1;
     }
 
     void OnDrawGizmos()
     {
         Gizmos.color = Color.green;
         Gizmos.DrawWireSphere(center, 0.5f);
     }
Comment
Add comment · Show 1 · 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 bsmx125 · Jun 05, 2019 at 08:00 PM 0
Share

Thnx but that doesn't fit well my code xd

avatar image
0

Answer by highpockets · Jun 05, 2019 at 04:38 PM

So the sphere collider is on the parent of all the neutrons and protons, correct?? In the first script, you are setting x y and z to 0 on every iteration. Try this:

  void SphereRadious()
      {
          //4/3 * pi * r^3 = V ; Vt =  V * (protons + neutrons) ; r^3 = Vt/(4/3 * pi) ; r = sqrt(Vt/(4/3 * pi))
          float atomVol = 4 / 3 * Mathf.PI * 1;
          float totalVol = atomVol * (protonsAmount + neutronsAmount);
          float r = Mathf.Pow(totalVol / ((4 / 3) * Mathf.PI), 1f/3f);
          sphereCollider.radius = r;
          radiousOfSpawn = r;
 
          //PROBLEM:
 
          float x = 0;
          float y = 0;
          float z = 0;
  
  
          for (int p = 0; p < protons.Length; p++)
          {
              for(int n = 0; n < neutrons.Length; n++)
              {
                  x += (protons[p].transform.localPosition.x + neutrons[n].transform.localPosition.x);
                  y += (protons[p].transform.localPosition.y + neutrons[n].transform.localPosition.y);
                  z += (protons[p].transform.localPosition.z + neutrons[n].transform.localPosition.z);
              }
          }
          float totalParticles = protonsAmount + neutronsAmount;
          x = x / totalParticles;
          y =  y / totalParticles;
          z =  z / totalParticles;
          sphereCollider.center = new Vector3(x, y, z);
      }

One thing that I can't wrap my head around is how you are not getting an error comparing an int to a float in your loop. I imagine protonsAmount/neutronsAmount are floats, but you compare them to ints in your for loop without casting. I changed that to protons.Length and neutrons.Length.

Comment
Add comment · Show 1 · 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 bsmx125 · Jun 05, 2019 at 07:59 PM 0
Share

Nah, the protonsAmount/neutronsAmount are ints xd

With that code it happens the same that with other code i've tried, although with < 4 it works fine (with your code and other i've done) when i spawn more it gets weird.

alt text

capture.png (93.2 kB)

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

201 People are following this question.

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

Related Questions

How to make two colliders or rigidbodies stick together upon collision, as if there was a glue on their surfaces? 3 Answers

Box collider vs Quad(mesh) collider 1 Answer

Detect if a non-trigger collider is inside another collider 1 Answer

Checking for collisions before changing layers 0 Answers

How to check is object directly next to another object? 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