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 mgcg12 · Jul 15, 2018 at 06:16 AM · c #

changing player position after scene load

Hi friends,

so, I have 2 scenes: a village and a dungeon scene. The player starts in the village. The dungeon is accessed by a door(collider that calls Level Manager to change the scene) in the village scene and vice versa.

On the door, I have put a "Desired spawn position" variable for the player to be in the new scene, when it changes.

When the player collides with the door inside the dungeon, the desired spawn position is saved on PlayerPrefs, and when the new scene loads, the player script reads the PlayerPrefs and I make a vector3 with the data. I can print these vector3 and they contain the information I want.

The door script is:

 public Vector3 playerDesiredPosition;
 public int desiredLevel;

 private void OnTriggerEnter(Collider other)
 {
 PlayerPrefs.SetString("PLAYER_POSITION_X", playerDesiredPosition.x.ToString("F2"));
 PlayerPrefs.SetString("PLAYER_POSITION_Y", playerDesiredPosition.y.ToString("F2"));
 PlayerPrefs.SetString("PLAYER_POSITION_Z", playerDesiredPosition.z.ToString("F2"));
 LevelManager.doorUsed = true;
 SceneManager.LoadScene(desiredLevel);
 }

When the new scene is loaded, the player script runs this method on Start() [its the last thing called on Start]:

 private void setPlayerPosition()
 {
 if (LevelManager.doorUsed == true)
 {
 print("door used");
 float playerPositionX = float.Parse(PlayerPrefs.GetString("PLAYER_POSITION_X"));
 float playerPositionY = float.Parse(PlayerPrefs.GetString("PLAYER_POSITION_Y"));
 float playerPositionZ = float.Parse(PlayerPrefs.GetString("PLAYER_POSITION_Z"));
 Vector3 desiredPosition = new Vector3(playerPositionX, playerPositionY, playerPositionZ);
 GetComponent().position = Vector3.zero;
 GetComponent().position = desiredPosition;
 LevelManager.doorUsed = false;
 }
 else if (LevelManager.doorUsed == false)
 {
 print("Game started, no door used");
 }

The problem is: the player changes its position to something a bit random. It's not the position I wanted. It spawns somewhere else in the terrain.

What could be happening?

Ps.: the player object has a rect transform, but if I do a transform.position I get the same behaviour

Ps2: if the default position of the player prefab is near the door, it works. If its far and 20 units up on Y, it doesnt

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How can I make my character move left or right when I touch and hold my android device and go back to center when I release the touch? 0 Answers

How can i get error when failed connect to server? 1 Answer

Null Refrence Expection 2 Answers

C # script taking values from one object and modifying another. 0 Answers

click on object then zoom in 0 Answers


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