Hey, I am new to programming but have been working on a project for a few months now. I am making a 2D game that is supposed to let you move between scenes like in an adventure game. I was using playprefs to store spawn locations for the next scene, but that has recently started becoming very unpredictable.
I have read in many places that I better way to handle this would be to create a singleton object to manage where the character spawns. Could anyone give me any resources that could guide me in how to go about this? I am still very new to all this still and am having a lot of trouble with trying to figure this out.
The script I have bee using is this one, and is attached to trigger objects that are places at both ends of my scene:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class DoorTrigger2 : MonoBehaviour {
public GameObject MyPlayer;
public int levelInt;
public float spawnX;
public float spawnY;
void OnTriggerEnter2D(Collider2D target)
{
if (target.gameObject.tag == "player")
PlayerPrefs.SetFloat ("spawnX", spawnX);
PlayerPrefs.SetFloat ("spawnY", spawnY);
SceneManager.LoadScene ("level" + levelInt);
}
void OnLevelWasLoaded ()
{
MyPlayer.transform.position = new Vector3 (PlayerPrefs.GetFloat ("spawnX"), PlayerPrefs.GetFloat ("spawnY"), 0);
//I recently tried to debug to see where the player was trying to spawn, found spawn points to often be far from where I was intending
Vector3 spawnPt = new Vector3 (PlayerPrefs.GetFloat ("spawnX"), PlayerPrefs.GetFloat ("spawnY"), 0);
Debug.DrawRay (MyPlayer.transform.position, spawnPt, Color.blue, 1.0f);
Debug.Log ("Line here");
}
}