Question by
agarm · Mar 10, 2021 at 05:24 PM ·
2d gameloadplayer movementscene-switching
2D player sliding after scene transition
I am having an issue getting my player to load scenes properly. I have a 2D grid-based movement game. Currently I have only two scenes. My player starts in the middle of SceneA. When they go to the transition, they appear where I want them to in SceneB. When I do the transition from SceneB back to SceneA, the player loads way off down the screen and slowly slides into the middle of SceneA instead of at my load point (the scene transition).
Here is my Scene Transition code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class SceneTransition : Interactable //inherit the OnTriggerEnter functions {
public string sceneToLoad;
public Vector2 playerPosition;
public VectorValue playerStorage;
public GameObject fadeInPanel;
public GameObject fadeOutPanel;
private float fadeWait = .5f;
private void Awake() //fades in on white screen
{
if(fadeInPanel != null)
{
GameObject panel = Instantiate(fadeInPanel, Vector3.zero, Quaternion.identity) as GameObject;
Destroy(panel, 1);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && inRange) //when you press space, fade out then load the new scene
{
//SceneManager.LoadScene(sceneToLoad);
StartCoroutine(FadeCo());
playerStorage.initialValue = playerPosition;
}
}
public IEnumerator FadeCo() //fade coroutine
{
if (fadeOutPanel != null) //starts the fade out
{
Instantiate(fadeOutPanel, Vector3.zero, Quaternion.identity);
}
yield return new WaitForSeconds(fadeWait);
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneToLoad); //load new scene
while (!asyncOperation.isDone)
{
yield return null;
}
}
}
Thanks in advance!
Comment