Hello again all!
I’m thrown off, not exactly sure what’s wrong here so…a question for the interwebs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CoM_Find : MonoBehaviour{
public float sysMass;
public Vector3 CoM;
//Gizmos
void OnDrawGizmos(){
//CoM
Gizmos.DrawWireSphere (CoM, 1f);
}
void Update(){
//Vars
CoM = Vector3.zero;
sysMass = 0f;
//Get Children // Objects involved with CoM
GameObject[] children = new GameObject[transform.childCount];
for (int i = 0; i < children.Length; i++){
children[i] = transform.GetChild(i).gameObject;
}
//Calculate CoM
foreach (GameObject gO in children){
CoM += gO.rigidbody.worldCenterOfMass * gO.rigidbody.mass;
sysMass += gO.rigidbody.mass;
}
CoM /= sysMass;
}
}
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?