- Home /
Zelda Room Switching & Preventing Duplicate Player Spawns from dontdestroyonload
I am making a 2D game that has multiple rooms (scenes), laid out in a 3 x 3 grid, player starts in the center one. Orange dots are exit portals.
Problem 1: When exiting a room and loading the next room, the player should appear in the new room on the opposite side of where he left.
For example, if player exits right, he should switch scenes to that room, but then be positioned on the LEFT side of that new room, indicating he just exited right (like in the old NES Zelda dungeons)
I tried saving the player vector2 position as a variable, then loading it and inverting either the X or Y, but got lost on how to know when to invert which number.
Problem 2: When going back to the START room, a second player appears. I understand from searching that this is because of my dontdestroyonload on the player, but don't know how to prevent the duplicate. I have the player in the hierarchy with a script that contains:
void Start()
{
DontDestroyOnLoad(this.gameObject);
}
I'm new to unity but I would put a spawn location on each side of the room and figure out a way to tag them (North, South, East, West.) Then when a scene is about to be loaded , specify which spawn location I want the player to appear based on which side the door was on that he/she exited. (Which would probably also just happen to be a spawn location). Just an idea. If the person exited on the west. The new scene should place him on the east. Etc
A sensible solution, since you can keep GameObjects between scene loading with DontDestroyOnLoad,
Answer by babasuter · Jun 22, 2020 at 06:31 PM
I found solutions to both of these problems. You can see them implemented by opening room "B2" in this Unity Github Repo
Problem 1: Here's the relevant part of the room switching code:
private void OnTriggerEnter2D(Collider2D other)
{
//FROM A1
if (other.gameObject.CompareTag("East") && SceneManager.GetActiveScene().name == "A1")
{
spawnLoc = new Vector2(-7, 0);
SceneManager.LoadScene("B1");
transform.position = spawnLoc;
}
if (other.gameObject.CompareTag("South") && SceneManager.GetActiveScene().name == "A1")
{
spawnLoc = new Vector2(0, 7);
SceneManager.LoadScene("A2");
transform.position = spawnLoc;
}
Problem 2: I read that singletons could be used, but I went with this solution, which just checks if there is already a GameManager, then destroys the extra one. There are 2 scripts, GameManager, and PlayerControllerNESW.
In the GameManager Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public GameObject player;
void Awake()
{
this.InstantiateController();
}
private void InstantiateController()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this);
}
else if (this != Instance)
{
Debug.Log("Destroying extra GM");
Destroy(this.gameObject);
}
}
void Start()
{
Debug.Log("Started");
Debug.Log("Player has not spawned. I'll make one for you.");
Instantiate(player, new Vector2(-4, 4), Quaternion.identity);
}
}
Answer by Soraphis · Apr 21, 2016 at 08:38 AM
As @Cherno said: you can use "DonDestroyOnLoad". e.g. for you player or whatever.
Do each room in its own scene. Create a "SwitchScene"-Script which knows the target scene, and target position.
when the player enters a trigger-zone:
load the new scene additive.
- teleport the player to its new position.
do some fancy stuff like camera swipe or whatever
unload the old scene.
in the editor (use the new multy-scene-edit feature) load both scene and position them correct to each other
maybe you need two different scripts for this job. e.g. when switching levels from indoor to outdoor locations.
Do you have a script that can be used to teleport the player?
Teleportation would be very nice to use ins$$anonymous$$d of having multiple scenes for the same room.
In this case, teleport just means updating the transform.position of the player.
What would be the best way to update the transform.position. The Unity C# just keeps changing. I get frustrated because all of the tutorials are outdated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075254im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Return to a specific scene & position 0 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer