revival/game/Assets/Scripts/Traps/FallawayFloor.cs

67 lines
1.8 KiB
C#
Raw Normal View History

2021-03-13 03:01:20 +01:00
using Ktyl.Util;
2021-03-05 17:24:50 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallawayFloor : MonoBehaviour
{
2021-03-02 20:47:28 +01:00
// Speed at which the object moves towards the ground.
public float speed;
2021-03-02 20:47:28 +01:00
// Time it takes for ogjecct to begin moving towards the ground.
public float fallAwayTime;
// Time taken for object to be destroyed.
public float destroyObjectTime;
public Material dissolve;
2021-03-05 17:24:50 +01:00
[SerializeField] private Renderer _renderer;
2021-03-13 03:01:20 +01:00
[SerializeField] private SerialFloat objectTimeScale;
2021-03-05 17:24:50 +01:00
Rigidbody rb;
private void Start()
{
2021-03-02 20:47:28 +01:00
// Get Rigidbody component.
rb = GetComponent<Rigidbody>();
}
private void OnTriggerEnter(Collider other)
{
2021-03-05 17:24:50 +01:00
if (other.CompareTag("Player"))
{
2021-03-02 20:47:28 +01:00
// Start the Destroy floor coroutine and switch to the dissolve material.
2021-03-13 03:01:20 +01:00
if (objectTimeScale != 0)
{
StartCoroutine(DestroyFloor());
_renderer.material = dissolve;
}
}
}
//The platform gets destroyed after the player resumes frozen time on a platform
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("Player"))
{
// Start the Destroy floor coroutine and switch to the dissolve material.
if (objectTimeScale != 0)
{
StartCoroutine(DestroyFloor());
_renderer.material = dissolve;
}
}
}
IEnumerator DestroyFloor()
{
2021-03-02 20:47:28 +01:00
// Take fallAwayTime, speed, and destroyObjectTime from editor and apply
yield return new WaitForSeconds(fallAwayTime);
rb.velocity = Vector3.down * speed;
2021-03-02 20:47:28 +01:00
yield return new WaitForSeconds(destroyObjectTime);
Destroy(gameObject);
}
}