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

54 lines
1.3 KiB
C#
Raw Normal View History

2021-03-05 17:24:50 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
2021-03-10 18:41:09 +01:00
using UnityEditor;
using UnityEngine;
2021-03-10 18:41:09 +01:00
[RequireComponent(typeof(Rigidbody))]
public class FallawayFloor : MonoBehaviour
{
// // 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;
public Material dissolve;
2021-03-10 18:41:09 +01:00
2021-03-05 17:24:50 +01:00
[SerializeField] private Renderer _renderer;
private Rigidbody rb;
private Vector3 initialPosition;
private void Start()
{
initialPosition = transform.position;
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.
StartCoroutine(Fall());
_renderer.material = dissolve;
}
}
private IEnumerator Fall()
{
// wait a moment
2021-03-02 20:47:28 +01:00
yield return new WaitForSeconds(fallAwayTime);
2021-03-10 18:41:09 +01:00
// fall
rb.velocity = Vector3.down * speed;
2021-03-10 18:41:09 +01:00
}
public void Reset()
{
transform.position = initialPosition;
rb.velocity = Vector3.zero;
}
}