- Home /
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);
}
}
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
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**
}
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.
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.
Your answer
Follow this Question
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