// Remove a fraction of Attachables. Add a force so they are blown away public void ExplodeAttached(float fractionToExplode = 0.5f) { int initialAttachCount = AttachCount; int toRemoveCount = (int)(fractionToExplode * initialAttachCount); // Copy attachables to an array. This avoids errors when looping over a list that is changing in size AttachableController[] attachableList = new AttachableController[initialAttachCount]; attached.CopyTo(attachableList); // Remove objects from the Player foreach (AttachableController attachableController in attachableList) { if (toRemoveCount <= 0) { break; } toRemoveCount--; // Detach the object from the Player and explode the object attachableController.Detach(this); Detach(attachableController); attachableController.rigidBody.AddExplosionForce(explodeDetachVelocity, attachableController.transform.localPosition, 3f, 3f, ForceMode.VelocityChange); } }
public void Attach(AttachableController attachableController) { if (!attached.Contains(attachableController)) { attached.Add(attachableController); Mass += attachableController.Mass; OnAttachedCountChanged(AttachCount); } }
public void Detach(AttachableController attachableController) { if (attached.Contains(attachableController)) { attached.Remove(attachableController); Mass -= attachableController.Mass; OnAttachedCountChanged(AttachCount); } }
public virtual void OnAttachableCollision(Collision collision, AttachableController attachableController) { attachableController.Attach(controller); }
public AttachableFreeState(AttachableController controller) : base(controller) { stateName = "Free"; }
// Objects in this state are attached to the player public AttachableAttachedState(AttachableController controller) : base(controller) { stateName = "Attached"; }