I can get the platform to descend and destroy it easily. I need to reset it back to its original state and position after a few seconds however. I have tried stopping the fall and transforming it using another trigger collider but nothing is working. Please help I’m at a standstill tell I find this out.
using UnityEngine;
using System.Collections;
public class FallingPlatforms : MonoBehaviour {
public GameObject Platform;
public Transform start;
private bool isFalling = false;
private float downSpeed = 0;
void OnTriggerEnter(Collider other)
{
{
if (other.gameObject.tag == "Player")
isFalling = true;
}
{
if (other.gameObject.tag == "PlatformRS")
isFalling = false;
}
}
void Update()
{
if (isFalling)
{
downSpeed += Time.deltaTime/17;
transform.position = new Vector3(transform.position.x, transform.position.y-downSpeed,
transform.position.z);
}
}
}
You can simply add a coroutine if you want to define a duration for the object to fall.
public float resetTimer = 5.0f; // time in seconds before reset.
private Vector3 initialPosition;
void Start()
{
initialPosition = transform.position;
}
// ...
if (other.gameObject.tag == "Player")
StartCoroutine(FallingPlatform());
// isFalling = true; // --- replaced line ---
// ...
IEnumerator FallingPlatform()
{
isFalling = true;
yield return new WaitForSeconds(resetTimer);
isFalling = false;
transform.position = initialPosition;
}
So, here’s what this does… First, you have a timer defined for how long the platform falls before resetting. This can be customized as desired, in case some platforms are intended to fall a very long distance first.
Second, I declare, then in Start() I define a value for the starting position of the platform. When it does reset its position, it needs to know where to go to.
In OnTriggerEnter(), I replaced the boolean variable state change with a call to the coroutine.
In the coroutine, it’s straightforward – Set the boolean so that the platform falls down in Update(). After “resetTimer” seconds have elapsed, change the boolean back and reset the position.