using UnityEngine; namespace DestroyIt { public class ReleaseObject : MonoBehaviour { public GameObject objectToRelease; public Vector3 angularVelocity; public Vector3 forceToAdd; private Vector3 _velocityLastUpdate; void Start() { _velocityLastUpdate = GetComponent().velocity; } void FixedUpdate() { Vector3 velocityChange = (GetComponent().velocity - _velocityLastUpdate) / GetComponent().mass; if (velocityChange.magnitude > .3f && objectToRelease != null) Release(); } void OnCollisionEnter(Collision collision) { if (collision.relativeVelocity.magnitude > 2f && objectToRelease != null) Release(); } private void Release() { objectToRelease.GetComponent().isKinematic = false; objectToRelease.GetComponent().angularVelocity = angularVelocity; objectToRelease.GetComponent().WakeUp(); if (forceToAdd != Vector3.zero) objectToRelease.GetComponent().AddForce(forceToAdd); Destroy(this); } } }