Unity 2d Moving the Player to a Checkpoint When They Die and Making Pickups Active Again
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Dwelling /
0
respawn all afterward player die or doing checkpoint
Hello to anybody.I building a 2D game and i take 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 brand checkpoints but when i lose only the player respawn)Cheers very much
My player Script(too contain the checkpoint of the thespian)
using System.Collections; using Organisation.Collections.Generic; using UnityEngine; public course PlayerController : MonoBehaviour { public Transform m_currMovingPlatform; public AudioClip touch on; AudioSource audio; bool doubleJump = false; public float maxSpeed; public Vector3 respawnPoint; bool grounded = simulated; 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 forcefulness to Y centrality of the rigidbody myRB.velocity = new Vector2 (myRB.velocity.x, jumpHeight); transform.parent = null; if (!doubleJump && !grounded) { doubleJump = truthful; } } // double leap reset if (grounded) { doubleJump = fake; } 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 movement = Input.GetAxis ("Horizontal"); myAnim.SetFloat ("speed", Mathf.Abs (movement)); 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.fourth dimension > 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 == "Money") audio.PlayOneShot (bear on, 0.7F); } void OnTriggerStay2D (Collider2D other) { if (other.tag == "Coin") Destroy (other.gameObject); } }
0 Replies
· Add your reply
- Sort:
Your respond
Welcome to Unity Answers
The best place to ask and answer questions well-nigh development with Unity.
To assist users navigate the site nosotros have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more than information.
Make certain to check out our Cognition Base for ordinarily asked Unity questions.
If you are a moderator, meet our Moderator Guidelines page.
We are making improvements to UA, run across the listing of changes.
Follow this Question
Related Questions
Source: https://answers.unity.com/questions/1300877/respawn-all-after-player-die-or-doing-checkpoint.html
You'll demand a reference for each plataform, so create a script Plataform or anything you want to call it, brand a Vector2 variable startPosition, and on the Get-go function ready it to exist = to its current position, then when the thespian loses just do a foreach loop trough all plataforms and set in that location transform.position = to startPosition.