Search Unity

How to load in scenes additively with a position relative to the camera?

Discussion in 'Scripting' started by Doh09, Jan 31, 2016.

  1. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    I'm making an endless runner game as a hobby project.

    As part of this I wish to load in scenes additively so they're there while the original scene is also there, and then unload the added scene after it has passed by, a bit similar to Jetpack Joyride.

    Currently, the player moves at a certain speed in one direction, he jumps to survive. The camera follows the players relative position.

    My issue is that when I add new scenes, I cannot get the objects of the scene being loaded to have its position relative to the player/camera, in other words it spawns at a wrong x/y position.

    I have attempted using this script in each of the objects of the scene:
    Code (csharp):
    1.  
    2. // Use this for initialization
    3. void Start () {
    4. //set position relative to camera.
    5. Camera camera = Camera2DFollow.cam;
    6. transform.position = new Vector2(transform.position.x + camera.transform.position.x, this.transform.position.y);
    7. }
    8.  
    The issue with this script is that it seems to move the spawned objects further and further away, so first spawn works alright, 2nd spawn spawns a bit further ahead, 3rd a bit more ahead and so on.

    How do i go about solving this?

    Thank you for reading this and hopefully helping me resolve the issue.

    Oh and please ask questions if I am not being clear enough.

    Here is a picture displaying the issue, the picture shows the game paused and zoomed out, this is after the game has run for around 20-30 seconds.

    unityquestion.png

    I also just noticed I get this error.
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. SetPositionRelativeToCamera.Start () (at Assets/Scripts/SetPositionRelativeToCamera.cs:12)
    4.  
    But I'm not sure why I get the NullReferenceException.

    The camera I refer to in Camera2DFollow is admittedly in a different scene than the object using the script, but as the camera variable being loaded is static, this should not be an issue, or?
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    If the map isnt big enough for floating point precision to be an issue you could build the whole world into a single scene then duplicate them and remove all objects that arnt in each scene. When a scene is added it uses the world positions that the gameobjects have saved in that scene.

    if floating point precision is an issue id recommend using a grid system keeping track of what scene is active. if the scenes have the same total dimentions you can shift everything back to 0 every time a scene is loaded essentially eliminating huge floats in transforms. I used this method creating huge space scenes.
     
  3. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    How do I know whether or not the map is big enough for floating point precision to be an issue?

    You are right, it is an option to have it all in one scene :), that would probably work.

    My only concern about that is that the reason I wish to have it split into more than 1 scene, is that I find it easier to edit and implement new sections that way.

    "if the scenes have the same total dimentions you can shift everything back to 0 every time a scene is loaded essentially eliminating huge floats in transforms. "

    That's a good idea! Think I might test that.
     
  4. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    if your not using rigidBodys for your character then you can go up to distances around 300,000 away from 0 before you start seeing some jitter. In my experience rigid bodies start to loose accuracy much earlier around 10 000. It probably varies. though. Its easy to test, just edit the transform position of player and camera at runtime to a huge number and see where you start to get jittering.
     
  5. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    I do use rigid bodies.

    And jittering is not really a thing I want to risk, so I'll try make a safe solution work :).