Basically, As stated above, I’m trying to add a house interior scene to my game where when a door is interacted with in the first scene (OverWorld) the player then gets teleported to a new scene (House 1 (Ik boring name, its gonna get changed later lol)) but it keeps it coordinates from the first scene which spawns it in the middle of the void…
Heres the current script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.AI; // Include NavMeshAgent for disabling it
public class DoorInteraction : MonoBehaviour
{
public GameObject interactImage; // UI image to show on the door
public string houseSceneName = "HouseScene"; // Name of the scene to load
public GameObject loadingScreen; // Reference to the loading screen Canvas
public Slider loadingSlider; // Reference to the slider for loading
public Transform player; // Reference to the player transform
public string spawnPointName = "PlayerSpawnPoint"; // Name of the spawn point in the house scene
public NavMeshAgent navMeshAgent; // Reference to the NavMeshAgent component
public MonoBehaviour playerController; // Reference to the player controller script (e.g., CharacterController, ThirdPersonController)
private bool isPlayerNearby = false;
void Update()
{
if (isPlayerNearby && Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(LoadHouseScene());
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
interactImage.SetActive(true); // Show the interaction image
isPlayerNearby = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
interactImage.SetActive(false); // Hide the interaction image
isPlayerNearby = false;
}
}
IEnumerator LoadHouseScene()
{
// Disable the NavMesh and Player Controller components before loading
DisablePlayerControls();
// Show the loading screen and set slider to 0
loadingScreen.SetActive(true);
loadingSlider.value = 0;
// Start async scene loading
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(houseSceneName);
asyncLoad.allowSceneActivation = false;
// Update slider as the scene loads
while (!asyncLoad.isDone)
{
loadingSlider.value = asyncLoad.progress;
// Activate the scene when it's fully loaded (progress >= 0.9f)
if (asyncLoad.progress >= 0.9f)
{
loadingSlider.value = 1f; // Full slider when scene is almost ready
asyncLoad.allowSceneActivation = true; // Activate the scene
}
yield return null;
}
// Once the scene is fully loaded, call the teleport function
SceneManager.sceneLoaded += OnSceneLoaded;
}
// This function is called once the new scene is loaded
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// Ensure the scene that is loaded is the one we expect
if (scene.name == houseSceneName)
{
// Teleport the player to the spawn point
TeleportPlayerToHouseSpawn();
}
// Unsubscribe from the event after use to avoid multiple calls
SceneManager.sceneLoaded -= OnSceneLoaded;
// Optionally hide the loading screen after teleportation
loadingScreen.SetActive(false);
}
void TeleportPlayerToHouseSpawn()
{
// Find the spawn point in the new scene (HouseScene)
GameObject spawnPoint = GameObject.Find(spawnPointName);
if (spawnPoint != null)
{
// Set the player's position to the spawn point location
player.position = spawnPoint.transform.position;
player.rotation = spawnPoint.transform.rotation; // Optional: if you want to match rotation too
}
else
{
Debug.LogError("Spawn point not found in the scene!");
}
// Re-enable the NavMesh and Player Controller after teleportation
EnablePlayerControls();
}
// Disable the NavMeshAgent and Player Controller during teleportation
void DisablePlayerControls()
{
if (navMeshAgent != null)
{
navMeshAgent.enabled = false; // Disable NavMeshAgent
}
if (playerController != null)
{
playerController.enabled = false; // Disable the player controller script (e.g., CharacterController, ThirdPersonController)
}
}
// Re-enable the NavMeshAgent and Player Controller after teleportation
void EnablePlayerControls()
{
if (navMeshAgent != null)
{
navMeshAgent.enabled = true; // Re-enable NavMeshAgent
}
if (playerController != null)
{
playerController.enabled = true; // Re-enable the player controller script
}
}
}
Edit 1: I have one character object with a camera. They both dont destroy on load so they can be used in multiple scenes. Not to sure what else to do.
Edit 2: I want to have multiple buildings avaliable that they can go into, and when they exit the building they teleport to out side the door. (Aka a spawn point at each door)
Edit 3: It currently just uses the postition that the character was last at in the first scene