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 GuiltyGai · Oct 13, 2017 at 01:41 PM · instantiaterespawndestroygameobject

Respawn function after character is destroyed

I'm making a simple bomberman game where after one character is destroyed, it respawns at the exact opposite location of the other character in terms of x and z. For example, if one character is at the bottom left corner of the map, the other would respawn at the upper right. I thought creating an if statement when the character is destroyed would work, but it doesn't seem so. Anyone know how to do so?

Here's the code for one of the character's movement

 public class Movement : MonoBehaviour {
 
     public GameObject character;
     private float speed = 2.0f;
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.D)) {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.A)) {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.W)) {
             transform.position += Vector3.forward * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.S)) {
             transform.position += Vector3.back * speed * Time.deltaTime;
         }
     }
 }

And here's the code for the bomb spawn and its explosion

 public class bombSpawn : MonoBehaviour {
     public GameObject bombPrefab;
     GameObject bombClone;
     public Transform pos;
     Vector3 spawn_pos;
     float x,y,z;
 
     void Start(){
         pos = transform;
     }
 
     void bomb(){
         x = pos.position.x;
         y = pos.position.y;
         z = pos.position.z - 1f;
         spawn_pos = new Vector3(x,y,z);
 
         bombClone = Instantiate (bombPrefab, spawn_pos, bombPrefab.transform.rotation) as GameObject;
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown ("left shift")) {
             bomb ();
         }
     }
 }


 [RequireComponent(typeof(AudioSource))]
 public class bomb_explode : MonoBehaviour {
 
     public float speed = .3f;
     Vector3 temp;
     public float timer = 3f;
 
     // Use this for initialization
     void Start () {
         AudioSource explosion = GetComponent<AudioSource>();
         explosion.PlayDelayed(3);
     }
     
     // Update is called once per frame
     void Update () {
         timer -= Time.deltaTime;
         temp = transform.localScale;
 
         SphereCollider sphereCollider = gameObject.GetComponent<SphereCollider> ();
 
         if (timer <= 0) {
             temp.x += .05f;
             temp.y += .05f;
             temp.z += .05f;
 
             sphereCollider.radius += .00000075f;
         }
 
         transform.localScale = temp;
 
         Destroy (gameObject, 5);
     }
 
     void OnTriggerEnter(Collider other){
         Destroy(other.gameObject);
     }
 }
 



Comment
Add comment · Show 1
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 GuiltyGai · Oct 13, 2017 at 01:44 PM 0
Share

The bomb scripts are separated at lines 25 and 30, don't know why they were connected.

Also, if possible, I'd like for the respawn function to be placed in the character movement scripts

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Yuvii · Oct 13, 2017 at 02:04 PM

Hey,

I would advice you to make a gameManager instead of putting the respawn code in the movement script. A game manager is simply an empty gameobject that will manage things in your game, like spawning things and especially players.

By the way, if the player dies it will (normally) be disabled (don't destroy/instantiate it for respawn, just use gameObject.SetActive(true/false)), so scripts on it won't work (disabled or destroyed).

On your gameManager, just put the players as variables, and when on dies, just disable it and use a little code like

 StartCoroutine(Respawn(somePlayer, somePosition, someTimeToRespawn));
 
 IEnumerator Respawn(GameObject Player, Vector3 Pos, float delayTime)
 {
   **some respawn code**
 }


Comment
Add comment · Share
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
0

Answer by keithloganbecker · Oct 13, 2017 at 02:14 PM

So I can not tell you how to implement this algorithm into your code, but still, here is the algorithm I would use.

if the screen coordinates are 800x800(x,y) pixels, and character 1 is at 400x100(x,y) (top, right) of screen, and you want the other character to appear at the bottom left of screen at (100, 400) pixels, then (did you see that relationship?) you might be able to just swap the x and the y position for the other character. That seems like it would work.

Comment
Add comment · Share
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
0

Answer by prefix · Oct 13, 2017 at 02:57 PM

I would agree to using a GameManager to hold this information, but as far as spawning opposite of enemy.. I would :

Create spawn points around the map. If say 4 (one at each corner) I would tell my transform upon death, which spawn point is the furthest from the enemy, or perhaps my local transform(if i always die near the other player), then have it spawn me at the furthest spawn point.

Comment
Add comment · Share
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

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

91 People are following this question.

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

Related Questions

Respawn Management 2 Answers

Help with error when using instantiate to respawn an enemy. 0 Answers

How to instantiate and destroy objects with 2d trigger 1 Answer

Mulitple spawn points with random seed. 3 Answers

Limiting respawns on scene 1 Answer


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