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 Boog1eMan · Nov 04, 2012 at 04:36 PM · positionloadlevelapplicationteleport

Teleporting/application.loadlevel

Hello. I'm making a hobby project in unity and I'v encountered a problem when going from one level to another. Heres a picture that sheds some light on what i mean:

alt text

Currently i only have 2 scenes; 1) main menu 2) level 1 & 2.

When i start the game through main menu, i get moved to a certain position in lvl 1 after that i can move between 1 and 2 by using trigger on collision+transform.position. Now I'm trying to expand by making more maps, but putting them into the same scene will generate too much lagg, so i moved the existing lvl 2 into its own scene and put it to use:

var destination :int;

function OnTriggerEnter(other : Collider) { if (other.tag == "Player") { Application.LoadLevel(destination); } }

Now comes the actual problem: When i go back into lvl 1 scene i teleport to the original starting location, when i should have teleported outside the entrance of lvl 2 (a cave). So basically i start the level 1 over.

This will become even bigger problem when i start making more maps as seen in the picture. Is there a way to teleport just like using transform.position method except between scenes or a way to use application.loadlevel(4) and add some extra parameters like if last map lvl 3 then instantiate spawnpoint at entrance of lvl 3 of 4th lvl(ugh hard to explain). Or any other ways that would be better that what im thinking of.

Thanx for help, sorry for the rant. ^_^

paint_dribble.jpg (13.2 kB)
Comment
Add comment · Show 3
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 asafsitner · Nov 04, 2012 at 07:48 PM 1
Share

You'll need to save the state of the level and load it when you go back to it.

Just teleporting to the entrance of the other level will not solve the 'restart' problem, i.e. all scripts and enemies and whatever will reset.

avatar image Boog1eMan · Nov 04, 2012 at 10:32 PM 0
Share

Hmm... but if you look at the picture above; if i start from lvl 1, go to lvl 3 and then 4 then back to 1 it still wont work. If i understood what you ment.

avatar image asafsitner · Nov 04, 2012 at 10:35 PM 0
Share

What I meant is that you have to save the state of the level when you exit it, like 'freezing' the level, and then 'resume' it when you get back to it, regardless of where you got there.

1 Reply

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

Answer by Michael CMS · Nov 05, 2012 at 02:18 PM

Imho use a KISS approach (Keep it simple sir ) : have a static variable for the last position on each level, then on the level loading set the position of the player at the last position of the level you are entering.

It's only 3 floats per level, not a huge overhead, and you will avoid allot of non-transparent behavior.

Comment
Add comment · Show 4 · 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 Boog1eMan · Nov 05, 2012 at 03:38 PM 0
Share

Hmm sounds like what im looking for. How would i use this practically? Can you give an example or reference page?

avatar image Michael CMS · Nov 05, 2012 at 08:41 PM 0
Share

Practically on your OnTrigger enter function you will save the current player position in a static Vector3 variable.

Then upon the load of a level, on a Awake function of a script that is attached to the player, you will set the player position to the static Vector3 variable according to that level. Here's some C# code sample (I'm C# coder)

class SavedPlayerPositions { static Vector3 Level1LastPlayerPosition = Level1StartPosition; static Vector3 Level2LastPlayerPosition = Level2StartPosition; ... //how many levels you got, you put the default start location as initial value }

// enter from level 1 to level 2 OnTriggerEnter(Collider other) { if (other.tag=="Player") { if (currentLevel=="Level1") { // probably allways true since this collider is in level 1 SavedPlayerPositions.Level1LastPlayerPosition = other.transform.position; } Application.LoadLevel(destination); } } // now you have the last position of the player saved // all you have to do is to modify the awake function of your player so the player is set to the location you want upon loading a level

class IPlaceThePlayerOnPosition : $$anonymous$$onobehavior { void Awake() { if (currentLevel=="Level1") { gameObject.transform.position = SavedPlayerPositions.Level1StartPosition; } else if (currentLevel="Level2") { gameObject.transform.position = SavedPlayerPositions.Level2StartPosition; } } }

Now if you run this it will initially place the player on the default positions of each level.

When you change the level the default position for that level will be set to the last position the player had.

When you return to the level, the player will be placed in the last position he was in the level.

Just make sure to disable the trigger for a while(since the last position will be on top of the trigger, and you might end up loading between level 1 and 2 all the time )

avatar image asafsitner · Nov 05, 2012 at 08:45 PM 0
Share

You can format code properly in a comment by writing it as an answer, formatting, then copying the text to the comment box. ^^

avatar image Michael CMS · Nov 05, 2012 at 09:42 PM 0
Share

Thanks for that mate :) . I'm new to unity answers. :).

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

12 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

Related Questions

FadeAndLoadLevel? 1 Answer

Application.LoadLevel wont work 0 Answers

MissingMethodException error? 0 Answers

Moving gameobject relative to resolution 1 Answer

Transform position? 2 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