hi i want to respawn my fallling platform when my player looses a life so he can use that platform again.
this is my code by the way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallingPlatform : MonoBehaviour {
private Rigidbody2D rbd;
public float fallplat;
private void Start()
{
rbd = GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.collider.CompareTag("Player"))
{
StartCoroutine(Fall());
}
}
IEnumerator Fall()
{
yield return new WaitForSeconds(fallplat);
rbd.isKinematic = false;
GetComponent<Collider2D>().isTrigger = true;
yield return 0;
}
}
I would have a list that your platforms get added to so if the player dies during the level, you just loop through the list and reset all your platforms.
You can always add all the ones that fall and then clear the list after reset. Or you could have all the platforms in the list to start and just loop through them all.
Brathnann:
I would have a list that your platforms get added to so if the player dies during the level, you just loop through the list and reset all your platforms.
You can always add all the ones that fall and then clear the list after reset. Or you could have all the platforms in the list to start and just loop through them all.
how can i do that? can you help me with the script?
i have already fixed it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallingPlatform : MonoBehaviour {
private Rigidbody2D rbd;
private BoxCollider2D boxcollider;
public float fallplat;
public float fallplaton;
public bool isFalling = false;
public Vector2 initialposition;
private void Awake()
{
rbd = GetComponent<Rigidbody2D>();
boxcollider = GetComponent<BoxCollider2D>();
}
private void Start()
{
initialposition = transform.position;
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("Player"))
{
Invoke("Fall", fallplat);
}
}
void Fall()
{
rbd.isKinematic = false;
boxcollider.isTrigger = true;
isFalling = true;
}
void respawn()
{
StartCoroutine(respawnco());
}
IEnumerator respawnco()
{
yield return new WaitForSeconds(fallplaton);
isFalling = false;
rbd.isKinematic = true;
boxcollider.isTrigger = false;
transform.position = initialposition;
rbd.velocity = Vector2.zero;
}
private void OnTriggerEnter2D(Collider2D col)
{
if(col.tag == "Death")
{
rbd.isKinematic = true;
boxcollider.isTrigger = false;
isFalling = false;
respawn();
}
}
}
1 Like
Perfect! Thank you for this solution, it solves my problem!