/// <summary> /// Called when a collider exits the volume. We stop tracking any rigid bodies. /// </summary> /// <param name="other">The collider that exited the volume.</param> private void OnTriggerExit(Collider other) { if (!other) { return; } // apply the layer mask limit. if (!layer.Contains(other.gameObject.layer)) { return; } // apply the tag filter. if (useFilterTag && other.tag != filterTag) { return; } Rigidbody rigidbody = other.GetComponent <Rigidbody>(); if (!rigidbody) { return; } int index = rigidBodies.FindIndex(r => r.rigidbody == rigidbody); TrackedRigidbody trackedRigidbody = rigidBodies[index]; rigidBodies.RemoveAt(index); // apply the gravity mode to the rigid body. switch (gravity) { case PhysicsVolumeGravityMode.ZeroGravity: rigidbody.useGravity = true; break; case PhysicsVolumeGravityMode.ZeroGravityRestore: rigidbody.useGravity = trackedRigidbody.hadGravity; break; } }
private void FixedUpdate() { // iterate the tracked rigid bodies in reverse. for (int i = rigidBodies.Count - 1; i >= 0; i--) { TrackedRigidbody trackedRigidbody = rigidBodies[i]; Rigidbody rigidbody = trackedRigidbody.rigidbody; // if the rigid body was deleted, stop tracking it. if (!rigidbody) { rigidBodies.RemoveAt(i); continue; } // apply the force to the rigid body. Vector3 appliedForce = forceSpace == PhysicsVolumeForceSpace.Relative ? transform.rotation * force : force; switch (forceMode) { case PhysicsVolumeForceMode.Force: rigidbody.AddForce(appliedForce, ForceMode.Force); break; case PhysicsVolumeForceMode.Impulse: rigidbody.AddForce(appliedForce, ForceMode.Impulse); break; case PhysicsVolumeForceMode.VelocityChange: rigidbody.AddForce(appliedForce, ForceMode.VelocityChange); break; case PhysicsVolumeForceMode.Acceleration: rigidbody.AddForce(appliedForce, ForceMode.Acceleration); break; } // apply the relative force to the rigid body. Vector3 appliedRelativeForce = relativeForceSpace == PhysicsVolumeForceSpace.Relative ? transform.rotation * relativeForce : relativeForce; switch (relativeForceMode) { case PhysicsVolumeForceMode.Force: rigidbody.AddRelativeForce(appliedRelativeForce, ForceMode.Force); break; case PhysicsVolumeForceMode.Impulse: rigidbody.AddRelativeForce(appliedRelativeForce, ForceMode.Impulse); break; case PhysicsVolumeForceMode.VelocityChange: rigidbody.AddRelativeForce(appliedRelativeForce, ForceMode.VelocityChange); break; case PhysicsVolumeForceMode.Acceleration: rigidbody.AddRelativeForce(appliedRelativeForce, ForceMode.Acceleration); break; } // apply the torque to the rigid body. Vector3 appliedTorque = torqueSpace == PhysicsVolumeForceSpace.Relative ? transform.rotation * torque : torque; switch (torqueForceMode) { case PhysicsVolumeForceMode.Force: rigidbody.AddTorque(appliedTorque, ForceMode.Force); break; case PhysicsVolumeForceMode.Impulse: rigidbody.AddTorque(appliedTorque, ForceMode.Impulse); break; case PhysicsVolumeForceMode.VelocityChange: rigidbody.AddTorque(appliedTorque, ForceMode.VelocityChange); break; case PhysicsVolumeForceMode.Acceleration: rigidbody.AddTorque(appliedTorque, ForceMode.Acceleration); break; } // apply the relative torque to the rigid body. Vector3 appliedRelativeTorque = relativeTorqueSpace == PhysicsVolumeForceSpace.Relative ? transform.rotation * relativeTorque : relativeTorque; switch (relativeTorqueForceMode) { case PhysicsVolumeForceMode.Force: rigidbody.AddRelativeTorque(appliedRelativeTorque, ForceMode.Force); break; case PhysicsVolumeForceMode.Impulse: rigidbody.AddRelativeTorque(appliedRelativeTorque, ForceMode.Impulse); break; case PhysicsVolumeForceMode.VelocityChange: rigidbody.AddRelativeTorque(appliedRelativeTorque, ForceMode.VelocityChange); break; case PhysicsVolumeForceMode.Acceleration: rigidbody.AddRelativeTorque(appliedRelativeTorque, ForceMode.Acceleration); break; } } }