Moving between scenes like an adventure game
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");
}
}
This is not an answer, but to help give you insight. Rouge like It is rogue game that when you reach end point a new scene transitions seamlessly with a fader.
Also, well organized and a Unity3D example.
Thanks! I will definitely check this out, it looks like it could help me with what I need!
Answer by $$anonymous$$ · Apr 08, 2016 at 10:35 PM
You can use colliders void OnTriggerEnter(Collider other) { Void OnTrigger Website Unity Scripting API
Or....
If you want to change scenes using keys, like I did, here is a script (JavaScript) for that
#pragma strict
function Start () {
}
function Update () {
if(Input.GetKeyDown(KeyCode.Alpha1)){
Application.LoadLevel("Guy");
}
}
Reply back to me if you need more help.
Your answer
Follow this Question
Related Questions
Door keeps rotating 0 Answers
Door scripting, changing scenes 2 Answers
empty object changes position when add the door object 0 Answers