- Home /
Respawning player not working
Hello Guys, I'm making a 2D game , and I am having some trouble making my player respawn when he falls. I have a script attached to my player, that makes basically 3 things: Kill the player when he falls off a platform and touches an invisible Collider2D that detects the player, create a new one on the spawning point and then move the camera towards the new player.
But I can't get that going. Whenever my player dies, the camera moves towards the spawning point and I can see that a Character(Clone) is created, but I can't see him on the Game window (Only the camera moving if i move my player) and he appears visible on the Scene Window as well...
Here's the code:
public class Respawn : MonoBehaviour {
public GameObject character; //a prefab of my player will be attached to the script
public Transform spawnPoint; // an empty gameObject will act as the spawnPoint
void OnTriggerEnter2D(Collider2D coll){
Destroy (coll.gameObject);
if (character) {
GameObject p = Instantiate (character, spawnPoint.position, Quaternion.identity) as GameObject;
CamController cam = Camera.main.gameObject.GetComponent<CamController> (); //The script that mo
cam.target = p.transform;
}
}
}
Hope you can help me!
FYI - you probably have other scripts that have Start/Awake GetComponent or drag/drop references to the player, but when you Destroy and then Respawn those are probably flaking out
Why destroy player instance? Disable it, move it to the respawn and re-enable it. Unless you are making permanence for the corpse...
Answer by largow · Apr 14, 2014 at 03:16 AM
Fixed it! It was something related to the camera! Dont know why, just changed the camera projection I was using. Remember, this is 2D, never use perspective, you don't want depth and stuff like that. Use ORTOGRAPHIC. Also made a simpler script of my camera, I attach it if you want it:
Code:
using UnityEngine;
using System.Collections;
public class CamaraController : MonoBehaviour {
public Transform target;
public float height = 3f;
public float distance = 3f;
public float damping = 5f;
// Update is called once per frame
void Update () {
Vector3 pos = target.TransformPoint(0,height,-distance);
transform.position = Vector3.Lerp (transform.position, pos, Time.deltaTime * damping);
}
}
Answer by glaucomorais · Apr 13, 2014 at 03:15 AM
Destroy (coll.gameObject); at line 7.
Probably you're destroying the player when it is instantiated. Tag it and make sure to not destroy player when it collides with the gameobject that this script is attached.