- Home /
Player Respawn Rotation Troubles
Hi, relatively new to this and just trying to get an idea of how to make a basic game. I'm trying to get a player prefab to spawn using a script that is attached to a camera that is showing the last location of where the player died, it's supposed to spawn them at a random spawn point that is currently just an empty gameobject with a tag "PlayerSpawner". However, upon the respawn, I have been unable to get it to face the correct direction unless I set the parent of the player as the spawn point, which works but bothers me since it may cause problems if the player is always a child of a spawn point. I have already tried replacing the "Quaternion.identity" with the transforms of the spawn point or just any different direction using "Quaternion.Euler" as well as removing the parent once the player hit the floor, but it all just resulted in the player facing forward in the global Z-axis and not forward in the direction I intended. any help would be appreciated and thanks for reading this rather poorly worded question.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeathcamController : MonoBehaviour
{
public float Respawntime;
GameObject[] Spawnpoint;
GameObject currentPoint;
private Transform SpawnpointLocal;
public GameObject Player;
int index;
void Start()
{
StartCoroutine(Respawner());
Spawnpoint = GameObject.FindGameObjectsWithTag("PlayerSpawner");
index = Random.Range(0,Spawnpoint.Length);
currentPoint = Spawnpoint[index];
SpawnpointLocal = currentPoint.transform;
}
IEnumerator Respawner()
{
yield return new WaitForSeconds(Respawntime);
var playerspawned = Instantiate(Player, SpawnpointLocal.position, Quaternion.identity) as GameObject;
playerspawned.transform.SetParent(SpawnpointLocal);
Destroy(gameObject);
}
}
IEnumerator Respawner()
{
yield return new WaitForSeconds(Respawntime);
var playerspawned = Instantiate(Player, SpawnpointLocal.position, SpawnpointLocal.rotation) as GameObject;
// No need to parent
// playerspawned.transform.SetParent(SpawnpointLocal);
Destroy(gameObject);
}
Should work
That should work but for some reason it doesn't. just tried it now to check if I hadn't mistyped at an earlier point but it still places me with the same rotation as if I had used Quaternion.Identity even though the spawnpoint it's spawning onto doesn't face that way. Thanks for the suggestion though.
Answer by tanmaykulkarni · May 03, 2021 at 11:40 AM
Hii, you are first instantiating the game object 'Player' and then using 'transform.SetParent()', instead you should use
GameObject yourObject = Instantiate(yourPositionVar, Quaternion.Identity, yourParentObject)
This sets the parent while spawning, this might solve your issue.