I’m trying to get my player to respawn upon colliding with the enemy but every time they die, they just appear somewhere far off in the level (I assume).
Here’s my script:
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour {
public GameObject player;
public Transform SpawnPoint;
void OnTriggerEnter (Collider col)
{
if (col.tag == "Player")
{
player.transform.position = SpawnPoint.transform.position;
}
}
}
I’ve looked online and this has been the closest one to work for me other than this single problem. Help?
You can't teleport your player because you destroy it with this script, unity can't teleport an object if this object is destroyed. You need to remove "GameObject.Destroy(other.gameObject);" or just remove this script. sorry for bad english
Your logic is a bit strange. First, this script should be on another object instead of the player.
Second, Why are you also using transform on the Spawnpoint. I imagine because of that the spawnpoint keeps transforming. And last, you don’t need public GameObject player if you reference it as col in your script.
I rewrote it a bit for you. (Your first script was close, but you forgot somethings)
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour {
public Vector3 SpawnPoint;
void OnTriggerEnter (Collider col)
{
if (col.tag == "Player")
{
col.transform.position = SpawnPoint;
}
}
}
Instead of public Transform SpawnPoint; I used Vector3. You can change its (x,y,z) in the inspector, what you fill for those values will be the spawnposition.
Then I changed player.transform.position = SpawnPoint.transform.position;to col.transform.position = SpawnPoint;this lets me transform the col to the spawnpoint position that we set earlier as a Vector3(x,y,z)
And at last I removed public GameObject Player; because this was in my eyes not needed.
I hope my modification on your code and my explanation have helped you a bit.
What should be removed (script(s) wise) and what scripts should be relocated? Where does this one go?
I am having problems, mostly because none of these have worked, I am making somewhat of a floor is lava type game, and when I set the player as a player tag and set the spawnpoint, it just sinks through the floor
Do you have any script using Destroy(); ?
– PerimetricWhat is your Respawn script attached to?
– JustinTheDevThe player.
– AlexTheHollowYou can't teleport your player because you destroy it with this script, unity can't teleport an object if this object is destroyed. You need to remove "GameObject.Destroy(other.gameObject);" or just remove this script. sorry for bad english
– PerimetricI REALLY need that script. It's the foundation of my game. Is there a way to work around it or change it to work together?
– AlexTheHollow