Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dimitrakis1992 · Jan 19, 2017 at 09:32 AM · c#2d gameplayerspawnpointscheckpoint

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);
     }
 }    

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dimitrakis1992 · Jan 19, 2017 at 07:56 PM 0
Share

Anyone Please?

avatar image EDevJogos · Jan 19, 2017 at 10:38 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges