Search Unity

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

Discussion in 'Editor & General Support' 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:
    // Use this for initialization
    void Start () {
    //set position relative to camera.
    Camera camera = Camera2DFollow.cam;
    transform.position = new Vector2(transform.position.x + camera.transform.position.x, this.transform.position.y);
    }

    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
     
  2. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    I also just noticed I get this error.

    NullReferenceException: Object reference not set to an instance of an object
    SetPositionRelativeToCamera.Start () (at Assets/Scripts/SetPositionRelativeToCamera.cs:12)

    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?
     
  3. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16