2021-03-05 17:24:50 +01:00
|
|
|
using System;
|
2021-02-25 00:41:33 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2021-03-17 19:04:31 +01:00
|
|
|
using Ktyl.Util;
|
2021-02-25 00:41:33 +01:00
|
|
|
using UnityEngine;
|
|
|
|
|
2021-03-10 18:41:09 +01:00
|
|
|
[RequireComponent(typeof(Rigidbody))]
|
2021-02-25 00:41:33 +01:00
|
|
|
public class FallawayFloor : MonoBehaviour
|
|
|
|
{
|
2021-03-10 19:26:33 +01:00
|
|
|
// // Speed at which the object moves towards the ground.
|
2021-02-25 01:51:11 +01:00
|
|
|
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;
|
2021-02-26 15:50:30 +01:00
|
|
|
public Material dissolve;
|
2021-03-10 18:41:09 +01:00
|
|
|
|
2021-03-05 17:24:50 +01:00
|
|
|
[SerializeField] private Renderer _renderer;
|
2021-03-17 19:04:31 +01:00
|
|
|
[SerializeField] private TrapSettings _settings;
|
2021-03-05 17:24:50 +01:00
|
|
|
|
2021-03-10 19:26:33 +01:00
|
|
|
private Rigidbody rb;
|
|
|
|
private Vector3 initialPosition;
|
2021-03-17 19:04:31 +01:00
|
|
|
private bool _triggered = false;
|
2021-02-25 01:51:11 +01:00
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
2021-03-10 19:26:33 +01:00
|
|
|
initialPosition = transform.position;
|
|
|
|
|
2021-03-02 20:47:28 +01:00
|
|
|
// Get Rigidbody component.
|
2021-02-25 01:51:11 +01:00
|
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
}
|
|
|
|
|
2021-03-17 19:04:31 +01:00
|
|
|
private void LateUpdate()
|
|
|
|
{
|
|
|
|
if (!_triggered) return;
|
|
|
|
|
|
|
|
if (_settings.FallawayFloor.CanRespawn)
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 01:51:11 +01:00
|
|
|
private void OnTriggerEnter(Collider other)
|
2021-02-25 00:41:33 +01:00
|
|
|
{
|
2021-03-17 19:04:31 +01:00
|
|
|
if (!_triggered && other.CompareTag("Player"))
|
2021-02-25 01:51:11 +01:00
|
|
|
{
|
2021-03-10 19:26:33 +01:00
|
|
|
StartCoroutine(Fall());
|
2021-02-25 01:51:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:26:33 +01:00
|
|
|
private IEnumerator Fall()
|
2021-02-25 01:51:11 +01:00
|
|
|
{
|
2021-03-17 19:04:31 +01:00
|
|
|
// TODO: trigger shake, maybe particle effect?
|
|
|
|
|
|
|
|
_triggered = true;
|
|
|
|
|
2021-03-10 19:26:33 +01:00
|
|
|
// wait a moment
|
2021-03-02 20:47:28 +01:00
|
|
|
yield return new WaitForSeconds(fallAwayTime);
|
2021-03-10 18:41:09 +01:00
|
|
|
|
2021-03-10 19:26:33 +01:00
|
|
|
// fall
|
2021-02-25 01:51:11 +01:00
|
|
|
rb.velocity = Vector3.down * speed;
|
2021-03-10 18:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
2021-03-17 19:04:31 +01:00
|
|
|
_triggered = false;
|
2021-03-10 19:26:33 +01:00
|
|
|
transform.position = initialPosition;
|
|
|
|
rb.velocity = Vector3.zero;
|
2021-02-25 00:41:33 +01:00
|
|
|
}
|
|
|
|
}
|