Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by blaziinq · Apr 10 at 08:35 PM · scene-switching

Choosing where a player spawns on the loading of a scene

First, I would like to say that I am new to unity so I'm not sure if this is an easy problem or not, I just can't find a solution. So, I have 1 scene called the village scene that contains a bunch of houses (that each have their own scenes) that I can enter via their doors using the below script that I got from someone else.

 public string sceneToLoad;
 public void OnTriggerEnter2D(Collider2D other){
     if(other.CompareTag("Player") && !other.isTrigger){
         SceneManager.LoadScene(sceneToLoad);
     }
 }

The problem isn't that, as that just takes you to where the player is positioned in the building that is loaded. From each of the scenes that are the interiors of the houses, if I apply that script to an exit, it can only put the player in one position - where it's positioned in the scene. How do I, on the player exiting the building scenes, position the player in a specific position in the village scene depending on what scene was just exited?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Caeser_21 · Apr 11 at 10:00 AM

Incase you want to teleport the player to the exact point from where he entered the house, you could try :

  public string sceneToLoad;
  public void OnTriggerEnter2D(Collider2D other){
      if(other.CompareTag("Player") && !other.isTrigger){
          float x = other.gameObject.transform.position.x;
          float y = other.gameObject.transform.position.y;
          float z = other.gameObject.transform.position.z;
          PlayerPrefs.SetFloat("X", x);
          PlayerPrefs.SetFloat("Y", y);
          PlayerPrefs.SetFloat("Z", z);
          SceneManager.LoadScene(sceneToLoad);
      }
  }

Then in the script where you load the 'Village' scene, add this :

 void Start()
 {
     if (PlayerPrefs.HasKey("X") && PlayerPrefs.HasKey("Z") && PlayerPrefs.HasKey("Z"))
     {
         float newX = PlayerPrefs.GetFloat("X");
         float newY = PlayerPrefs.GetFloat("X");
         float newZ = PlayerPrefs.GetFloat("X");
 
         Player.transform.position = new Vector3(newX, newY, newZ);
     }
 }
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 DevManuel · Apr 11 at 09:24 AM

One suggestion could be to save the name of the current "house scene" (for example using prefabs). Then when you load your "village scene" simply read this variable and place your player where you want by checking some conditions:

 private void SaveData() {
    PlayerPrefs.SetString("houseScene", SceneManager.GetActiveScene().name);
    Debug.Log("data saved");
 }
     
 private string LoadData() // load stored data
 {
    string houseScene = "";
    if (PlayerPrefs.HasKey("houseScene")) {
       houseScene = PlayerPrefs.GetString("houseScene");
       Debug.Log("data loaded!");
    }else{
       Debug.Log("no saved data!");
    }
    return houseScene;
 }
     
 void Start(){
    string houseScene = loadData();
    switch (houseScene){  // name of last house scene
            case "house1":
               // move the player to target position
               player.positon = new Vector3(10,0,0); 
               break;
            case "house2":
               player.positon = new Vector3(10,10,0); 
               break;   
            default:
               player.positon = new Vector3(0,0,0); 
               break;
     }
 }
 

The idea is to save your data when entering your "house scene" and loading it when entering your "village scene". Hope it helps you, happy coding, Manuel

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

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

137 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 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 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 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 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 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 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 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 avatar image

Related Questions

Unity UI screen transitions 1 Answer

how to save scene when switch another scene? 2 Answers

[Unet] Player object loses authority on scene switch 1 Answer

Don't destroy On Load make object not interactable in new scene 0 Answers

Unity scene IF statement not working? 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