Advertisement
Hygcgggnngff

freeze hammer antoca

Aug 3rd, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class FreezeHammerScript : MonoBehaviour
  4. {
  5.     // Duration for which the object will remain frozen
  6.     public float freezeDuration = 5f;
  7.  
  8.     void OnTriggerEnter(Collider other)
  9.     {
  10.         // Check if the object has a Rigidbody to freeze
  11.         Rigidbody rb = other.GetComponent<Rigidbody>();
  12.  
  13.         if (rb != null)
  14.         {
  15.             // Start the freezing process
  16.             StartCoroutine(FreezeObject(rb));
  17.         }
  18.     }
  19.  
  20.     private IEnumerator FreezeObject(Rigidbody target)
  21.     {
  22.         // Save the original state of the object
  23.         Vector3 originalVelocity = target.velocity;
  24.         bool wasKinematic = target.isKinematic;
  25.  
  26.         // Freeze the object
  27.         target.velocity = Vector3.zero;
  28.         target.isKinematic = true;
  29.  
  30.         // Optional: Change material or appearance to indicate freezing
  31.         Renderer renderer = target.GetComponent<Renderer>();
  32.         if (renderer != null)
  33.         {
  34.             renderer.material.color = Color.cyan; // Example: change color to cyan
  35.         }
  36.  
  37.         // Wait for the duration of the freeze
  38.         yield return new WaitForSeconds(freezeDuration);
  39.  
  40.         // Unfreeze the object
  41.         target.isKinematic = wasKinematic;
  42.         target.velocity = originalVelocity;
  43.  
  44.         // Optional: Revert material or appearance after unfreezing
  45.         if (renderer != null)
  46.         {
  47.             renderer.material.color = Color.white; // Example: revert to original color
  48.         }
  49.     }
  50. }
  51.  
Tags: Script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement