Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class FreezeHammerScript : MonoBehaviour
- {
- // Duration for which the object will remain frozen
- public float freezeDuration = 5f;
- void OnTriggerEnter(Collider other)
- {
- // Check if the object has a Rigidbody to freeze
- Rigidbody rb = other.GetComponent<Rigidbody>();
- if (rb != null)
- {
- // Start the freezing process
- StartCoroutine(FreezeObject(rb));
- }
- }
- private IEnumerator FreezeObject(Rigidbody target)
- {
- // Save the original state of the object
- Vector3 originalVelocity = target.velocity;
- bool wasKinematic = target.isKinematic;
- // Freeze the object
- target.velocity = Vector3.zero;
- target.isKinematic = true;
- // Optional: Change material or appearance to indicate freezing
- Renderer renderer = target.GetComponent<Renderer>();
- if (renderer != null)
- {
- renderer.material.color = Color.cyan; // Example: change color to cyan
- }
- // Wait for the duration of the freeze
- yield return new WaitForSeconds(freezeDuration);
- // Unfreeze the object
- target.isKinematic = wasKinematic;
- target.velocity = originalVelocity;
- // Optional: Revert material or appearance after unfreezing
- if (renderer != null)
- {
- renderer.material.color = Color.white; // Example: revert to original color
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement