Hello everyone. I’m trying to get a falling platform to respawn in the same spot after it falls away and a time limit has passed. The code I have allows to the platform to fall, but when it respawns it falls away immediately without my Player touching it. I want the platform to appear at the original spot but not fall away unless the player lands on it again. Any help would be appreciated! Thanks!
Here’s my code:
public class FallingPlatform : MonoBehaviour {
public Rigidbody2D myRigidbody;
public GameObject newPlatform;
private Vector3 startPos;
public float fallDelay;
public float respawnDelay;
// Use this for initialization
void Start () {
myRigidbody = GetComponent ();
startPos = transform.position;
}
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.tag == “Player”) {
StartCoroutine(PlatformFall());
}
}
void OnCollisionExit2D(Collision2D other){
if (other.gameObject.tag == “Player”) {
StopCoroutine(PlatformFall());
}
}
IEnumerator PlatformFall(){
yield return new WaitForSeconds (fallDelay);
myRigidbody.isKinematic = false;
GetComponent ().isTrigger = true;
myRigidbody.constraints = RigidbodyConstraints2D.None;
StartCoroutine(RespawnPlatform());
}
IEnumerator RespawnPlatform(){
yield return new WaitForSeconds (respawnDelay);
Instantiate (newPlatform);
myRigidbody.isKinematic = true;
transform.position = startPos;
GetComponent ().isTrigger = false;
myRigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
}
}