GravityObjectHandler GetMaxAttractor() { GravityObjectHandler thisGravityHandler = GetComponent <GravityObjectHandler>(); GravityObjectHandler[] gravityObjects = FindObjectsOfType <GravityObjectHandler>(); float maxAttraction = 0; GravityObjectHandler maxAttractor = null; foreach (GravityObjectHandler gravityObject in gravityObjects) { if (gravityObject == thisGravityHandler) { continue; } Vector3 direction = gravityObject.rb.position - transform.position; float distance = Vector3.Magnitude(direction); distance = Mathf.Clamp(distance, 1f, Mathf.Infinity); float attraction = (gravityObject.rb.mass / (distance * distance)); if (attraction > maxAttraction) { maxAttraction = attraction; maxAttractor = gravityObject; } } return(maxAttractor); }
public GravityObjectData(GravityObjectHandler gravityObject) { position = gravityObject.transform.position; velocity = gravityObject.initalVelocity; showFuturePath = gravityObject.showFuturePath; if (gravityObject.useDensity) { float totalVolume = 1; foreach (MeshFilter meshFilter in gravityObject.gameObject.GetComponentsInChildren <MeshFilter>()) { Vector3 objectSize = meshFilter.transform.localScale; float volume = objectSize[0] * objectSize[1] * objectSize[2]; totalVolume *= Utils.VolumeOfMesh(meshFilter.sharedMesh) * volume; } gravityObject.rb.mass = totalVolume * gravityObject.density; } mass = gravityObject.rb.mass; frozen = gravityObject.rb.isKinematic || !gravityObject.moveByGravity; affectGravity = gravityObject.affectGravity; }