private void ExplodeAll() { List <Slice2D> results = Slicer2D.ExplodeAll(sliceLayer); if (addForce == true) { foreach (Slice2D id in results) { foreach (GameObject gameObject in id.gameObjects) { Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> (); if (rigidBody2D) { float sliceRotation = Vector2D.Atan2(new Vector2D(0, 0), new Vector2D(gameObject.transform.position)); Rect rect = Polygon2D.CreateFromCollider(gameObject).GetBounds(); Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * addForceAmount / 10f, Mathf.Sin(sliceRotation) * addForceAmount / 10f), rect.center); } } if (sliceResultEvent != null) { sliceResultEvent(id); } } } }
private void Explode(Vector2 position) { if (fixedJoint != null) { Destroy(fixedJoint); } transform.SetParent(null); List <Slice2D> results = Slicer2D.ExplodeAll(BreakableManager.Instance.Slice2DLayer); if (addedExplosionForceOnBreak > 0) { foreach (Slice2D id in results) { foreach (GameObject gameObject in id.gameObjects) { var joint = gameObject.GetComponent <FixedJoint2D>(); if (joint != null) { Destroy(joint); } Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D>(); if (rigidBody2D) { float sliceRotation = Vector2D.Atan2(new Vector2D(position), new Vector2D(gameObject.transform.position)); Rect rect = Polygon2D.CreateFromCollider(gameObject).GetBounds(); Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * addedExplosionForceOnBreak, Mathf.Sin(sliceRotation) * addedExplosionForceOnBreak), rect.center); } } } } }