- Home /
Remember player position / room translation
Ok, I have been searching around and found a few ideas but none seem to work.
What I am doing in I have the player on the main scene enter a building, the building scene loads and I need the player to start at the location near the door. When the player exits the room, I need the player to start back where he was before he entered the room.
Please help.
Answer by username707 · Jul 20, 2013 at 06:43 AM
Can you set trigger on the exit area? Or constantly check for a player position range?
Answer by You! · Jul 20, 2013 at 06:02 AM
In JS...
public var pos : Vector3;
var default : Vector3;
var player : Transform; //Place the player transform here in the inspector
function Start () {
DontDestroyOnLoad(transform.gameObject);
}
function OnLevelWasLoaded () {
if(Application.loadedLevelName = "FancyNameOfMainScene"){
if (position = null){
player.position = default;
}
else {
player.position = pos;
}
}
}
"pos" is the variable which contains the position which the player will load to, "default" contains the default loading position of the character, and "player" is the character. "pos" is public so that another script (like the one which you use to load the room) can access it and change it to whatever is required or desired by using ScriptName.pos
"DontDestroyOnLoad" ensures that this script and the object it is attached to will still exist when the main scene is loaded again, making sure that the script comes to good use.
Ok I can't get this to work right, here is my code: //PlayerPOS.cs using UnityEngine; using System.Collections;
public class PlayerPOS : $$anonymous$$onoBehaviour {
public Vector3 pos;
public Vector3 defaultPos;
Transform player;
// Use this for initialization
void Start () {
DontDestroyOnLoad(transform.gameObject);
}
// Update is called once per frame
void Update () {
}
void OnLevelWasLoaded()
{
if(Application.loadedLevelName == "Door1")
{
if(player.position == null)
{
player.position = defaultPos;
Debug.Log("PlayerPOS 'Null'");
}
else
{
player.position = pos;
Debug.Log("PlayerPOS 'Set'");
}
}
}
}
//PlayerColl.cs
void OpenDoor1()
{
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "Press O to open door.";
if(Input.Get$$anonymous$$eyDown("o"))
{
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "You opened the door.";
Application.LoadLevel(door1);
PlayerPOS.pos.y = 1.320962;
PlayerPOS.pos.x = 15.24762;
PlayerPOS.pos.z = 18.65867;
Debug.Log("Loading POS Done");
}
}
Is the name of the main scene (where you go through the door into the room) "Door1"? The name of the main scene should be there ins$$anonymous$$d of "Door1"; the code from lines 19 to 35 is meant only to set the player position when the main level is loaded.
Ok I changed the loadedLevelName == "Compass", which is my main scene. Now if I add the positions to the var pos in the inspector, it works but when I try to script it using:
PlayerPOS.pos.y = 1.320962;
PlayerPOS.pos.x = 15.24762;
PlayerPOS.pos.z = 18.65867;
I get: Assets/Scripts/PlayerColl.cs(61,35): error CS0120: An object reference is required to access non-static member `PlayerPOS.pos'
for each axis.
You need to reference the object which the script which the script is located. It would be easiest to do this:
PlayerPOS savePosition = GameObject.Find("NameOfPlayerPOSScript").GetComponent<PlayerPos>();
savePosition.pos.y = 1.320962;
savePosition.pos.x = 15.24762;
savePosition.pos.z = 18.65867;
That way, you are referencing the actual script in the scene, ins$$anonymous$$d of the script in general. (Referencing the script in general would be similar to referencing a $$anonymous$$athf function - it does something, but it isn't saved.)
I made the change here:
//PlayerColl.cs void OpenDoor1() { GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "Press O to open door."; if(Input.Get$$anonymous$$eyDown("o")) {
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "You opened the door.";
Application.LoadLevel(door1);
PlayerPOS savePosition = GameObject.Find("PlayerPOS").GetComponent<PlayerPos>();
savePosition.pos.y = 1.320962;
savePosition.pos.x = 15.24762;
savePosition.pos.z = 18.65867;
Debug.Log("Loading POS Done");
}
}
Now I am getting these errors:
Assets/Scripts/PlayerColl.cs(61,95): error CS0246: The type or namespace name `PlayerPos' could not be found. Are you missing a using directive or an assembly reference?
Assets/Scripts/PlayerColl.cs(63,42): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffixf' to create a literal of this type
Assets/Scripts/PlayerColl.cs(64,42): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffixf' to create a literal of this type
Assets/Scripts/PlayerColl.cs(65,42): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffixf' to create a literal of this type
Thanks for your help with this, I am still very new with Unity :)
Answer by rddragon · Jul 21, 2013 at 06:08 PM
I made the change here: //PlayerColl.cs void OpenDoor1() { GameObject.Find("OnScreenMSG").guiText.text = "Press O to open door."; if(Input.GetKeyDown("o")) {
GameObject.Find("OnScreenMSG").guiText.text = "You opened the door.";
Application.LoadLevel(door1);
PlayerPOS savePosition = GameObject.Find("PlayerPOS").GetComponent<PlayerPos>();
savePosition.pos.y = 1.320962;
savePosition.pos.x = 15.24762;
savePosition.pos.z = 18.65867;
Debug.Log("Loading POS Done");
}
}
Now I am getting these errors:
Assets/Scripts/PlayerColl.cs(61,95): error CS0246: The type or namespace name PlayerPos' could not be found. Are you missing a using directive or an assembly reference? Assets/Scripts/PlayerColl.cs(63,42): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffix f' to create a literal of this type Assets/Scripts/PlayerColl.cs(64,42): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffix f' to create a literal of this type Assets/Scripts/PlayerColl.cs(65,42): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffix `f' to create a literal of this type
Thanks for your help with this, I am still very new with Unity :)
Firstly, this should either be a comment, or an edit to the original question (added to the bottom of the question).
The last three errors are simple enough; just add "f" behind the decimal numbers. From what I can tell, this is only something required in C# for float variables, to convert a normal "double" decimal to a "float" decimal.
The first error is slightly different, though... Do you have the PlayerPOS script added to a Gameobject in the Compass scene? It must be on a Gameobject to be in use. Just add an empty Gameobject (with whatever name you choose) and add the script to it. Be sure to change the string in GameObject.Find("ObjectName") to match the name of the Gameobject. If the script is attached to a Gameobject named "PlayerPOS", change the name, as this might be causing the error, as well.
I think I may need to go back to the beginning, these are both attached to the player character. Here is my Collision script:
//PlayerColl.cs
using UnityEngine;
using System.Collections;
public class PlayerColl : $$anonymous$$onoBehaviour {
GameObject currentDoor;
public string door1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit hit;
if(Physics.Raycast (transform.position, transform.forward,
out hit, 3)) {
if(hit.collider.gameObject.tag=="door1"){
OpenDoor1();
currentDoor = hit.collider.gameObject;
//Debug.Log("Hit Door");
}
if(hit.collider.gameObject.tag=="door1In"){
OpenDoor1In();
currentDoor = hit.collider.gameObject;
}
}
else
{
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "";
}
}
void OpenDoor1()
{
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "Press O to open door.";
if(Input.Get$$anonymous$$eyDown("o"))
{
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "You opened the door.";
PlayerPOS savePosition = GameObject.Find("3rd Person Controller").GetComponent<PlayerPOS>();
savePosition.pos.y = 1.320962f;
savePosition.pos.x = 15.24762f;
savePosition.pos.z = 18.65867f;
Debug.Log("Loading POS Done");
Application.LoadLevel(door1);
}
}
void OpenDoor1In()
{
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "Press O to open door.";
if(Input.Get$$anonymous$$eyDown("o"))
{
GameObject.Find("OnScreen$$anonymous$$SG").guiText.text = "You opened the door.";
PlayerPOS savePosition = GameObject.Find("3rd Person Controller").GetComponent<PlayerPOS>();
savePosition.pos.y = 0.3263679f;
savePosition.pos.x = 321.7976f;
savePosition.pos.z = 278.5175f;
Debug.Log("Loading POS Door1In");
Application.LoadLevel(door1);
}
}
}
Here is my PlayerPOS script:
//PlayerPOS.cs
using UnityEngine;
using System.Collections;
public class PlayerPOS : $$anonymous$$onoBehaviour {
public Vector3 pos;
public Vector3 defaultPos;
Transform player;
// Use this for initialization
void Start () {
DontDestroyOnLoad(transform.gameObject);
}
// Update is called once per frame
void Update () {
}
void OnLevelWasLoaded()
{
if(Application.loadedLevelName == "Compass")
{
if(player.position == null)
{
player.position = defaultPos;
Debug.Log("PlayerPOS 'Null'");
}
else
{
player.position = pos;
Debug.Log("PlayerPOS 'Set'");
Debug.Log(pos);
}
}
}
}
It only works when I had set the pos number in the inspector, can you please check it and let me know whet I am doing wrong.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Instantiate bullet towards Player position 1 Answer
swap between 3 gameobjects mid game 0 Answers
Stabilising users view in Unity VR (Fove HMD) 0 Answers
Saving System? 1 Answer