Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Center of Mass, small issue. Any help appreciated!

Discussion in 'Scripting' started by darkAbacus247, Dec 17, 2014.

  1. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    Hello again all!

    I'm thrown off, not exactly sure what's wrong here so...a question for the interwebs:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class CoM_Find : MonoBehaviour{
    7.     public float sysMass;
    8.     public Vector3 CoM;
    9.  
    10.     //Gizmos
    11.     void OnDrawGizmos(){
    12.         //CoM
    13.         Gizmos.DrawWireSphere (CoM, 1f);
    14.     }
    15.  
    16.     void Update(){
    17.         //Vars
    18.         CoM = Vector3.zero;
    19.         sysMass = 0f;
    20.      
    21.         //Get Children // Objects involved with CoM
    22.         GameObject[] children = new GameObject[transform.childCount];
    23.         for (int i = 0; i < children.Length; i++){
    24.             children[i] = transform.GetChild(i).gameObject;
    25.         }
    26.      
    27.         //Calculate CoM
    28.         foreach (GameObject gO in children){
    29.             CoM += gO.rigidbody.worldCenterOfMass * gO.rigidbody.mass;
    30.             sysMass += gO.rigidbody.mass;
    31.         }
    32.         CoM /= sysMass;
    33.  
    34.     }
    35. }
    36.  
    Merging ideas from HarvestR's code, this works if I move the objects manually in the inspector (you can see the Center of Mass position update), but does not work if I move the objects through script...

    What have I done wrong?
     
    Last edited: Dec 18, 2014
  2. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    Bump.
    Sorry, really wont to get back to work : )
     
    Last edited: Dec 18, 2014
  3. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    Pity bump?