My game layout is similar to Stardew Valley and Zelda style. My player enters buildings by interacting with a Collider2D placed at the door. My player is a prefab with DontDestroyOnLoad.
I would like to set my player in a specific transform position indoor once the scene of the building is loaded.
I have also made a black fading animation for the scene transition.
Right now, with this script, my player’s new position glitches and changes immediately and very briefly in the village scene before the fading animation and new scene has time to change. However, it does correctly place the player in the desired position once the new scene is fully loaded too.
So my problem is that I do not know where to place (transform.position = new Vector3(xPosition, yPosition, 0)) or what to add to my script that would make the player’s position changes only after the new scene is fully loaded.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public Animator transition;
public float transitionTime = 1f;
public string levelName;
public int Index;
public float xPosition;
public float yPosition;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
StartCoroutine(LoadLevel());
other.transform.position = new Vector3(xPosition, yPosition, 0);
}
}
IEnumerator LoadLevel()
{
transition.SetTrigger("Start");
yield return new WaitForSeconds(transitionTime);
SceneManager.LoadScene(Index);
}
}