void OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.tag == "Graviton") { coll.gameObject.GetComponent <Shatter>().ShatterFromCenter(body.velocity.magnitude); GravityGlobal.RemoveGravityObject(gameObject); Destroy(gameObject); } }
/// <summary> /// Replaces current sphere with subspheres. /// Subspheres are forced outward from current sphere's center. /// </summary> /// <param name="shatterStrength">Magnitude of force scattering the subsphere.</param> public void ShatterFromCenter(float shatterStrength) { CreateSubPieces(); var currentVelocity = body.velocity; foreach (var piece in piecesContainer.GetComponentsInChildren <Rigidbody2D>()) { var currentSphereVelocity = new Vector3(currentVelocity.x, currentVelocity.y, 0); var calculatedSubpiecesVelocity = currentSphereVelocity / newPiecesCount; piece.AddForce(((piece.transform.position - transform.position) * shatterStrength + calculatedSubpiecesVelocity), ForceMode2D.Impulse); } GravityGlobal.RemoveGravityObject(gameObject); Destroy(gameObject); }