- Home /
How can I update the players location?
I am trying to create a button that allows the user to restart the level. The easiest way I can see this being accomplished would be to move the player back to the start of the level. Below I have included the code I am using to try and update the player's location.
public void Repawn() { Debug.Log("Calling Respawn!"); Vector3 pos = new Vector3(4, 4, 0); Player.transform.position = pos; }
If the player's location was the same place every time this would be easy because I could just call the active scene and this would get reset. However each time the scene is called a random room and spawn are created. I have included some of the code used to accomplish this below. My first goal is to simply hard code values like the code above does and get the player to successfully move.
//spawn player at spawnpoint of first room in roomArray if(i == 0) { Room.Cell spawnPoint = roomArray[i].GetCellByType(Room.CellType.Spawn_Point); if (spawnPoint != null) { position = spawnPoint.getGameObject().transform.position; Instantiate(playerPrefab, position, Quaternion.identity); cameraPrefab = Instantiate(cameraPrefab, new Vector3(), Quaternion.identity); Debug.Log("Position: " + position); } }
Answer by VoidPhoenix96 · Feb 23, 2021 at 04:22 AM
You should make a Transform reference and create an empty gameObject in the scene, that way you can assign the emptyGameobject to the variable whenever you want and then just teleport the player to it when you want, like so:` Player.transform.position = spawnPoint.position `