- Home /
respawn all after player die or doing checkpoint
Hello to everyone.I building a 2D game and i have a problem.I want that when the player make checkpoint and if he lose, all the moved objects would respawn in their starting locations(enemies,falling platforms.)How to do that?(My player make checkpoints but when i lose only the player respawn)Thank you very much
My player Script(also contain the checkpoint of the player)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Transform m_currMovingPlatform;
public AudioClip impact;
AudioSource audio;
bool doubleJump = false;
public float maxSpeed;
public Vector3 respawnPoint;
bool grounded = false;
float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight;
public Transform gunTip;
public GameObject bullet;
float fireRate = 0.5f;
float nextFire = 0f;
// Use this for initialization
void Start ()
{
audio = GetComponent<AudioSource> ();
myRB = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator> ();
respawnPoint = transform.position;
facingRight = true;
}
// Update is called once per frame
void Update ()
{
if ((grounded || !doubleJump) && Input.GetButtonDown ("Jump")) {
//not on the ground
myAnim.SetBool ("isGrounded", false);
//add jump force to Y axis of the rigidbody
myRB.velocity = new Vector2 (myRB.velocity.x, jumpHeight);
transform.parent = null;
if (!doubleJump && !grounded) {
doubleJump = true;
}
}
// double jump reset
if (grounded) {
doubleJump = false;
}
if (Input.GetAxisRaw ("Fire1") > 0)
fireRocket ();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, groundLayer);
myAnim.SetBool ("isGrounded", grounded);
myAnim.SetFloat ("VerticalSpeed", myRB.velocity.y);
float move = Input.GetAxis ("Horizontal");
myAnim.SetFloat ("speed", Mathf.Abs (move));
myRB.velocity = new Vector2 (move * maxSpeed, myRB.velocity.y);
if (move > 0 && !facingRight) {
flip ();
} else if (move < 0 && facingRight) {
flip ();
}
}
void OnCollisionEnter2D (Collision2D coll)
{
if (coll.gameObject.tag == "Platform") {
m_currMovingPlatform = coll.gameObject.transform;
transform.SetParent (m_currMovingPlatform);
}
}
void OnCollisionExit2D (Collision2D coll)
{
if (coll.gameObject.tag == "Platform") {
m_currMovingPlatform = null;
}
}
void flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void fireRocket ()
{
if (Time.time > nextFire) {
nextFire = Time.time + fireRate;
if (facingRight) {
Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
} else if (!facingRight) {
Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 180f)));
}
}
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "cleaner") {
transform.position = respawnPoint;
}
if (other.tag == "Checkpoint") {
respawnPoint = other.transform.position;
}
if (other.tag == "Coin")
audio.PlayOneShot (impact, 0.7F);
}
void OnTriggerStay2D (Collider2D other)
{
if (other.tag == "Coin")
Destroy (other.gameObject);
}
}
You'll need a reference for each plataform, then create a script Plataform or anything you want to call it, make a Vector2 variable startPosition, and on the Start function set it to be = to its current position, then when the player loses just do a foreach loop trough all plataforms and set there transform.position = to startPosition.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Jump Function C# hassle! 0 Answers
Trying to make the camera follow the player but stop at the edge. 1 Answer
Collider not registering hits? 4 Answers