Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rddragon · Jul 20, 2013 at 04:26 AM · positionplayerroom

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rddragon · Jul 20, 2013 at 03:59 PM 0
Share

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");
                 
         }
                     
     }
avatar image You! · Jul 20, 2013 at 04:51 PM 0
Share

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.

avatar image rddragon · Jul 20, 2013 at 06:21 PM 0
Share

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.

avatar image You! · Jul 20, 2013 at 11:29 PM 0
Share

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.)

avatar image rddragon · Jul 21, 2013 at 06:08 PM 0
Share

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 :)

avatar image
0

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image You! · Jul 21, 2013 at 08:15 PM 0
Share

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.

avatar image rddragon · Jul 21, 2013 at 11:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges