2025-02-28 17:46:02 -05:00
|
|
|
using System.Collections;
|
2025-04-16 19:57:54 -04:00
|
|
|
using UnityEngine; using Game; using Music; using Player;
|
|
|
|
|
|
|
|
|
|
namespace Game
|
|
|
|
|
{
|
2025-02-28 17:46:02 -05:00
|
|
|
|
|
|
|
|
public class FallPlatform : MonoBehaviour
|
|
|
|
|
{
|
2025-03-28 17:39:07 -04:00
|
|
|
public float fallDelay = 2f; // Delay before the platform falls
|
|
|
|
|
public float resetDelay = 4f; // Delay before the platform resets
|
2025-02-28 17:46:02 -05:00
|
|
|
|
|
|
|
|
bool falling;
|
|
|
|
|
Rigidbody2D rb;
|
2025-03-07 11:29:09 -05:00
|
|
|
Vector3 defposition;
|
2025-02-28 17:46:02 -05:00
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2025-03-07 11:29:09 -05:00
|
|
|
defposition = transform.parent.position;
|
2025-03-03 11:58:00 -05:00
|
|
|
rb = transform.parent.GetComponent<Rigidbody2D>();
|
2025-02-28 17:46:02 -05:00
|
|
|
}
|
2025-03-28 17:39:07 -04:00
|
|
|
private void OnTriggerEnter2D(Collider2D collision) // Makes platform fall when player or another platform touch it
|
2025-02-28 17:46:02 -05:00
|
|
|
{
|
2025-04-07 18:56:43 -04:00
|
|
|
try
|
2025-02-28 17:46:02 -05:00
|
|
|
{
|
2025-04-07 18:56:43 -04:00
|
|
|
if (collision.transform.childCount != 0 && !falling && (collision.gameObject.CompareTag("Player") || collision.transform.GetChild(0).TryGetComponent(out FallPlatform _)))
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(FallAfterDelay());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Error in FallPlatform: " + e.Message);
|
2025-02-28 17:46:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
private IEnumerator FallAfterDelay() // Sets platform to fall and respawn
|
2025-02-28 17:46:02 -05:00
|
|
|
{
|
|
|
|
|
falling = true;
|
|
|
|
|
yield return new WaitForSeconds(fallDelay);
|
|
|
|
|
rb.bodyType = RigidbodyType2D.Dynamic;
|
2025-03-05 21:08:52 -05:00
|
|
|
yield return new WaitForSeconds(resetDelay);
|
2025-03-07 11:29:09 -05:00
|
|
|
transform.parent.GetComponent<Animator>().SetTrigger("respawn");
|
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
|
Respawn();
|
2025-02-28 17:46:02 -05:00
|
|
|
}
|
2025-03-05 21:08:52 -05:00
|
|
|
|
2025-03-06 20:59:22 -05:00
|
|
|
//only resets the object script is attached to, need to fix so platform will reset with fall trigger object
|
2025-03-07 11:29:09 -05:00
|
|
|
// Use transform.parent to get the object it's attatched to
|
2025-03-28 17:39:07 -04:00
|
|
|
private void Respawn() // Resets the platform position
|
2025-03-07 11:29:09 -05:00
|
|
|
{
|
|
|
|
|
falling = false;
|
|
|
|
|
rb.bodyType = RigidbodyType2D.Static;
|
|
|
|
|
transform.parent.position = defposition;
|
|
|
|
|
transform.parent.rotation = Quaternion.identity;
|
|
|
|
|
}
|
2025-02-28 17:46:02 -05:00
|
|
|
}
|
2025-04-16 19:57:54 -04:00
|
|
|
}
|