maxhacker11

FallingPlatform.cs

Aug 28th, 2023
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | Source Code | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class FallingPlatform : MonoBehaviour
  5. {
  6.     [SerializeField] private float fallDelay = 1f;
  7.     [SerializeField] private float destroyDelay = 2f;
  8.  
  9.     private bool falling = false;
  10.  
  11.     [SerializeField] private Rigidbody2D rb;
  12.  
  13.     private void OnCollisionEnter2D(Collision2D collision)
  14.     {
  15.         // Avoid calling the coroutine multiple times if it's already been called (falling)
  16.         if (falling)
  17.             return;
  18.  
  19.         // If the player landed on the platform, start falling
  20.         if (collision.transform.tag == "Player")
  21.         {
  22.             StartCoroutine(StartFall());
  23.         }
  24.     }
  25.  
  26.     private IEnumerator StartFall()
  27.     {
  28.         falling = true;
  29.  
  30.         // Wait for a few seconds before dropping
  31.         yield return new WaitForSeconds(fallDelay);
  32.  
  33.         // Enable rigidbody and destroy after a few seconds
  34.         rb.bodyType = RigidbodyType2D.Dynamic;
  35.         Destroy(gameObject, destroyDelay);
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment