Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Blackmens · May 30, 2016 at 05:14 AM · positionplayerscenesswitchdonotdestroy

Switching scences without loosing players position

Hey guys!

I'm working on my first game in Unity and I have a big problem.

There are 2 worlds in this game and in each world there are 5 houses you can walk in. So my big problem is, how can i make a script, in which the postition of my player is saved? Every time when I leave the houses I start at the beginning and I have already tried it with DontDestroyOnThis and saving the players position, as explained here in the forum, but nothing worked.

I don't know if i have to make a new script with dontdestroyonthis and connect it to the Player or do i have to put the code inside of the "FristPersonController" script. By the way I'm using the Unity Standard Asset First Person Controller.

Please can someone tell me the easiest way to manage this. The level switches are made with function OnTriggerEnter () and Application.LoadLevel (). Maybe I can write something in the triggers to realize that? Please help me I'm in a hurry and thanks a lot.

Comment
Add comment · Show 1
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 Blackmens · May 31, 2016 at 04:54 PM 0
Share

Is there nobody who can help me? Please...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zodiarc · May 31, 2016 at 04:56 PM

There´s a function called DontDestroyOnLoad or similar. This should do the trick.

see http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

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 Mmmpies · May 31, 2016 at 05:19 PM

Guess they didn't read that you'd tried that! :D

OK DontDestroyOnLoad is what you need BUT it's not the complete solution, for that you have to record the scene just before you load the next one. So put a variable in DDOL that will still know what the last scene was.

Then when the next scene loads you check what scene you came from. I just had a list of empty gameObjects and when I changed scenes I move the player instantly to the right position/rotation depending on the previous scene.

EDIT should have said really - you need Application.loadedLevelName to record the scene name just before you load the next one. Store that in Don't destroy and then pick the correct location for your player using if or case depending on how many you have.

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 Blackmens · May 31, 2016 at 05:53 PM 0
Share

O$$anonymous$$ thanks a lot, that really sounds good, but as i said I'm not good in program$$anonymous$$g. So maybe if you could give me an exact example that would really help out, if it's not to much trouble for you?

avatar image Mmmpies Blackmens · May 31, 2016 at 06:36 PM 0
Share

Well it looks like that's getting deprecated anyway so in your DDOL script hold a public int

 public int lastLevel;

Then before you call for another Scene set that variable from the script that loads the level. I'll call the script that is DDOL GameSettings...

 public GameSettings theGSscript;
 
 
 public void DoorClickedOn()
 {
      theGSscript.lastLevel = Scene$$anonymous$$anager.GetActiveScene ().buildIndex;
      // Now load the next Scene

Now I'm being clever here and the empty GameObject I create is tagged Spawn with a number after it, So Spawn0, Spawn1 etc.

When the scene load you just need to get that int from the DDOL file (along with all your saved player data). Then do this...

                 int i = theGSscript.lastLevel;
         string SpawnPoint = "Spawn" + i.ToString ();
         GameObject playerspawn = GameObject.FindGameObjectWithTag(SpawnPoint);
         player.transform.position = playerspawn.transform.position;

So you just tag the empty GameObject Spawn2 if you know you'll be entering from the Scene with index 2 and it'll move your player to the right location.

You might also want to do the rotation. And that last bit of code should be done AFTER you instantiate the player. It means setting up an empty GO even if there's only one entrance but it's really not that much of a hardship.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to Hide/Show 3d text according to player's position? 0 Answers

How to prevent the player to move beyond certain x-position value 3 Answers

Player teleport not working 0 Answers

How can I flip the player based on the position of the mouse cursor?,How can I flip my player based on the cursor's location? 0 Answers

Move Character in the way he is facing.,Make character move in way he is facing. 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