Change scene when character reaches edge of scene

Updated on May 23, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on May 21, 2018

Hi! I am making a 2D game where the character can move in the x and y direction but the camera remains stationary and I would like to change the scene when the character reaches the edge of the current scene. I am new to Unity & C# so bear with me if this is devilishly simple and I just don’t know.

I have read up on the top Google hits for 2D camera bounds, something about Orthographic Position, or WorldToPoint, if I am remembering correctly. The hits were very jargon technical so I am looking for a simpler explanation of how to declare a public variable in the script editor that can have the screen’s bounds passed in. Or, I tried creating a public GameObject so I could pass in the Camera to reference Camera.OrthographicPosition, but when I declare the public GameObject, the Unity software does not update the variable field in the script component, and continues to display my previous public variable–a Transform (previously just passing in the Transform of the camera and using a radial distance from the camera to bound the scene transition event; this doesn’t work because the screen is a rectangle!). I also get the error “The name cameraPos does not exist in the current context!” As if the Unity software is not updating according to the script.

My understanding is that the Transform of the Camera only provides position, scale, and rotation, and you’d have to do some fancy math to see what portion of the screen the camera projects onto using its distance from the 2D game background along the Z axis, along with the camera’s field of view in degrees (which I also don’t know how to access). I think orthographic position is a simpler way to do this, but I’m unclear what variables to define and what to pass into the script.

Any help, even minor nitpicks with my terminology as I am still learning and want to do and refer to things correctly, are mondo appreciated!

Thank you!

  • Liked by
Reply
0 on May 23, 2018

I reccemend putting a object with a collider on the edge of the camera just out of frame. then make it so that when you collide with the object it changes the scene.

 

  • Liked by
Reply
Cancel
0 on May 23, 2018

That’s what I was going to say!  Make the object invisible too.  Make the sprite a trigger.

  • Liked by
Reply
Cancel