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 Dwight_P · Jan 12, 2015 at 07:08 AM · sceneswarp

Waypoint/Warp Between Scenes

Good day! I am having a small problem, and I am really hoping someone can point me in the right direction on how to achieve my goal correctly. I do not think I am far off, but I could be wrong. ANYWAYS what I am trying to do is build a relatively modular waypoint/warp script to transfer the player character between scenes. The overall goal is to have the script setup so that it can be used to warp between points within the same scene, or jump to another scene entirely while still having control of the exact transform position after changing scenes. Here is what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class Waypoint : MonoBehaviour {
 
     [SerializeField]
     private string _location;
     [SerializeField]
     private bool _active = false;
     [SerializeField]
     private Vector3 _position = new Vector3(0f,0f,0f);
 
     public GameObject _player; 
     
     void OnTriggerEnter( Collider collider ) {
 
         if(_active == true){
             if(Application.loadedLevelName == _location || _location == string.Empty)
             {
                 collider.transform.position = _position;
             } else {
                 Application.LoadLevel (_location);
             }
         }
 
         _player = collider.gameObject;
     }
 
     void Awake() {
         DontDestroyOnLoad (_player);
     }
 }

For the most p[art everything is working as I would like it to. If the location field is empty, or populated with the current scenes name, it than uses the position vertex to "warp" to a different point within the same scene. My problem lies when I try to change scenes. I have the player character gameobject inserted into each zone, so the character changes scenes, and can be controlled, but I am unable to programmably change his position within the new scene after the scene loads. I am wondering, how does one go about this? The only time I have EVER got this to work is when I passed a variable to Awake(), but the program did not like it. Even then, only the character game object moved, and not any of the camera game objects that are suppose to spawn with it.

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

1 Reply

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

Answer by AndresBarrera · Jan 12, 2015 at 09:03 AM

You can try using DontDestroyOnLoad on your character:

If you always start at the same level/scene you can delete the character object in the other scenes, or else you will find duplicates every time a new scene is loaded when a character from the previous one is not being destroyed.

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 Dwight_P · Jan 12, 2015 at 04:46 PM 0
Share

That is not the issue. The problem is that after a new scene loads I am unable to move the character to a specific location in the new scene. The character always retains the previous Vector points from where it was standing in the last scene. Even been trying to do something like this:

Script 1 - WaypointController (On Character)

 using UnityEngine;
 using System.Collections;
 
 public class WaypointController : $$anonymous$$onoBehaviour {
     
     private int triggerCount = 0;
     
     void OnTriggerEnter( Collider collider ) {
         WaypointHolder wp = collider.gameObject.GetComponent<WaypointHolder>();
         
         if( wp != null ) {
             if(wp._active == true){
                 triggerCount++;
                 if(Application.loadedLevelName == wp._location || wp._location == string.Empty)
                 {
 
                 } else {
                     if(this.camera != null)
                     {
                         this.camera.transform.parent = null;
                     }
 
                     DontDestroyOnLoad (this.gameObject);
                     Application.LoadLevel (wp._location);
                 }
             }
         }
     }
     
     void OnTriggerExit( Collider collider ) {
         WaypointHolder wp = collider.gameObject.GetComponent<WaypointHolder>();
         
         if( wp != null ) {
             triggerCount--;
             if( triggerCount <= 0 ) {
                 this.transform.position = wp._position;
             }
         }
     }
 }


Script 2 -WaypointHolder (On Waypoint GameObject)

 using UnityEngine;
 using System.Collections;
 
 public class WaypointHolder : $$anonymous$$onoBehaviour {
     public string _location;
     public bool _active = false;
     public Vector3 _position = new Vector3(0f,0f,0f);
 }


Still, it does not work. An example could be my primary warp gate. The gateway Vector in my main scene is (126, 200, 40), and when I warp to the village the character stays on that Vector point, even if I set the new Vector points on the Waypoint Holder.

avatar image Scribe · Jan 12, 2015 at 05:50 PM 1
Share

you could use OnLevelWasLoaded to wait until you've loaded a new level before setting the position:

for example attach this script to your character:

 using UnityEngine;
 using System.Collections;
 
 public class WarpControl : $$anonymous$$onoBehaviour {    
     void Awake(){
         DontDestroyOnLoad(gameObject);
         ApplicationExtension.transform = transform;
     }
     
     void OnLevelWasLoaded(int l){
         ApplicationExtension.OnLevelWasLoaded(l);
     }
 }
 
 public static class ApplicationExtension{
     static public Transform transform;
     static Vector3 position;
     static int lastLevel = 0;
     
     public static void Warp(int l, Vector3 pos){
         if(l == lastLevel){
             return;
         }
         if(l > Application.levelCount-1){
             return;
         }
         position = pos;
         Application.LoadLevel(l);
     }
     
     public static void OnLevelWasLoaded(int l){
         transform.position = position;
         lastLevel = l;
     }
 }

and then you should be able to call

 ApplicationExtension.Warp(1, new Vector3(10, 0.5, 3));

to load level 1 at position Vector3(10, 0.5, 3). It will not do anything if the level passed is out of range.

You should have a start level set up where you have your character instantiated and nothing else, and you should NEVER revisit this level as it would create a second version of your singleton character!

Hope that helps

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 teleport to different scenes? 2 Answers

objects not reappearing 2 Answers

click gameobject, go to the next scene? 2 Answers

How to put seperate scenes together into one game 1 Answer

NGUI Button some how unclickable 3 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