- Home /
Loading player location in a scene
I am working on a game with multiple rooms and scenes. When you go through a door, a new scene loads in the next room, but when you go back through the same door to the previous room, I have to use an entirely new duplicate scene with an altered player position in front of that door. The whole system is controlled by triggers activated by GameObject colliders with a specific name.
Instead of having up to 14 identical scenes with different player locations, is there a way that I only have to have 1 scene per room?
In other words, when you go back through a door, can I load the same scene, but change the player location according to the door you just came from?
I hope what I am asking makes sense. I've tried googling and the Unity Forums but haven't found a suitable answer. Any help would be greatly appreciated. Thank you.
smashes head into keys Okay so after hours or coding, debugging, googling. Screw it I think Fable had it right, I'm just going to make duplicate scenes with different data in a progression. WHY IS IT SO HARD TO $$anonymous$$A$$anonymous$$E A JAVASCRIPT THAT TRAC$$anonymous$$S PLAYER POSITION IN-BETWEEN ALL SCENES.
Answer by TwitchBlade · Aug 30, 2014 at 07:22 PM
Could you save something in Player prefs like and in and say character one came from scene 2 through door three then when you load the scene just check and see where the player pos should be on load
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public GameObject player;
private bool newRoom;
private int prevDoor;
private int prevRoom;
// Use this for initialization
void Start () {
prevDoor = PlayerPrefs.GetInt("prevDoor");
prevRoom = PlayerPrefs.GetInt("prevRoom");
// predefined location
player.transform.position = Vector3();
}
// Update is called once per frame
void Update () {
//saveing which door and room you went to
if (/*player walks through door*/ newRoom)
{
PlayerPrefs.SetInt("prevDoor",3);
PlayerPrefs.SetInt("prevRoom",2);
}
Application.LoadLevel(level);
}
}
something like that
I have no experience with PlayerPrefs. I have tried using it before, but not in this instance. I honestly spent 2 months trying to get PlayerPrefs to work by watch tutorials, visiting the Docs, and the Unity Community. I can't get it right, haha. Basically, I'm trying to avoid PlayerPrefs for now if I can. I'll give it a fair try though.
Answer by GraviterX · Aug 30, 2014 at 09:15 PM
The only way to make this easier that I know of is to make animations and use scripting to open the doors. Then you can have multiple rooms in one scene.
I will include my script below. Add a trigger around the door. The player has to stand inside this trigger in order to open the door. You can also add an audiosource to make a sound play when they open the door. Also, rename the animation to your animation name.
var AudioFile : AudioClip;
function OnTriggerStay (other : Collider) {
if (Input.GetButtonDown("Fire1")){
animation.Play("door");
audio.clip = AudioFile;
audio.Play();
}
}
I hope this helps.
Thank you. I've been considering trying this. I'll let you know if it works out for me. Thank you again.
Your answer
Follow this Question
Related Questions
Loading player location in the same scene 3 Answers
Click Object to change scene 2 Answers
Restarting a Scene 1 Answer
Network - Load For Late Players 1 Answer