- Home /
Load Scene on Either Edge of Camera Collision
Hi everybody, I am making a 2D infinite runner game, and the Player has to keep running to the right, because the camera is also going to the right, and I am trying to make it so that when the player touches the edge of either side of the screen, it will load a Game Over scene, and I don't know how.
Thanks for the help!
$$anonymous$$gest you show us what you have tried so far, and where it fails, or where you get stuck. This general gets better responses.
Well, I tried to make it so that when the player touches a platform, on the left side of the camera, which the platform is following the camera and that when the player touches the platform, it loads a Game Over scene. Here is the script:
using UnityEngine; using System.Collections; using UnityEngine.Scene$$anonymous$$anagement;
public class DestroyerScript : $$anonymous$$onoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Debug.Break ();
}
if(other.gameObject.name == "RedGiant")
{
Application.LoadLevel ("Game Over"); }
else
{
Destroy (other.gameObject);
}
}
}
$$anonymous$$y game over scene is called Game Over. Nothing happens when I press play. There are no errors. But then I figured out that the player can collide with the camera, some how, and I don't hav any script for it and don't know what to do
You haven't specified whether your game is 2D or 3D this makes it very difficult to give u an appropriate answer, if the game were 3D i would place Spherecasters at the end of all my levels 2D and i would place overlap circles ins$$anonymous$$d.
Answer by Johat · Apr 20, 2016 at 06:16 AM
Hi.
One approach to this, is just to add a BoxCollider or similar attached to the camera just off of the edge you want to trigger the game over.
This may as well just be a trigger than a full collision. (You'll need to make sure either the player or the collider has a RigidBody attached to it -- you'll likely want to check the box that says "Kinematic" and you'll probably want to uncheck the box that says "Use Gravity" too.)
Then you could attach a script such as this to the trigger:
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameOverTrigger : MonoBehaviour
{
public string GameOverScene;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("PLAYER_TAG")
{
SceneManager.LoadScene(GameOverScene);
}
}
}
Of course, you could do it the other way around too and check if the wall hit the player (rather than the player hitting the wall).
NOTE:
Not sure if you have 2D or 3D Colliders... If you have 3D, the above will work. But for 2D Colliders, you need to use OnTriggerEnter2D instead, with the parameter type being Collider2D.
(This isn't to do with whether your gameplay is 2D or 3D, rather what type of Colliders you have attached to your GameObjects.)
Personally, I would be tempted to forego the manual setup of a trigger and just check the actual condition I care about: "did the player leave the left of the screen?". WorldToScreenPoint or similar would be ideal for this.
Something like this attached to your Camera should do that:
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameOverCheck : MonoBehaviour
{
public string GameOverScene;
// You could automatically find this in Start or Awake if you preferred
public Transform Player;
// How far ahead of the player's position (typically the centre) we should check to be off-screen
public float CheckOffset;
// We need to have a reference to the Camera component
private Camera _camera;
private void Start()
{
_camera = GetComponent<Camera>();
}
private void Update()
{
// This just is just to check a little further ahead of the player's position, since this position is normally the centre and you probably want to player to be completely off-screen
// This assumes the player's right is pointing in the direction they are moving, but if this isn't the case you might need to change this
// Let's store this world offset in a local variable
Vector3 worldOffset = CheckOffset * Player.right;
// Calculate the left screen position to check
Vector3 leftPositionToCheck = Player.position + worldOffset;
Vector3 leftScreenPosition = _camera.WorldToScreenPoint(leftPositionToCheck);
// Calculate the right screen position to check
// This time we want to offset to the left to make sure the player is completely off-screen to the right
Vector3 rightPositionToCheck = Player.position - worldOffset;
Vector3 rightScreenPosition = _camera.WorldToScreenPoint(rightPositionToCheck);
// screenPosition is in pixels, where x (along the width of the screen) goes from 0f at the very left to the width at the very right
// If the x value of the left position we're checking is less than 0f, then we're definitely off-screen!
// Also, if the x-value of the right position we're checking is greater than the screen width, then we're definitely off-screen!
if (leftScreenPosition.x < 0f || rightScreenPosition.x > Screen.width)
{
SceneManager.LoadScene(GameOverScene);
}
}
}
Either should work fine. Let me know how it goes =).
(Here's the reference for Screen.width. You may also be interested in Screen in general, to see what else is available to you.
(Excuse any typos, I'm on a tablet.)
There are errors (in game exceptions) WHILE the game is running. Errors like this :
$$anonymous$$issingComponentException: There is no 'Camera' attached to the "Patch Pirate" game object, but a script is trying to access it.
You probably need to add a Camera to the game object "Patch Pirate". Or your script needs to check if the component is attached before using it.
UnityEngine.Camera.WorldToScreenPoint (Vector3 position)
DestroyerScript.Update () (at Assets/DestroyerScript.cs:27)
and this
UnassignedReferenceException: The variable Player of DestroyerScript has not been assigned.
You probably need to assign the Player variable of the DestroyerScript script in the inspector.
UnityEngine.Transform.get_position ()
DestroyerScript.Update () (at Assets/DestroyerScript.cs:25)
and this
$$anonymous$$issingComponentException: There is no 'Camera' attached to the "RedGiant" game object, but a script is trying to access it.
You probably need to add a Camera to the game object "RedGiant". Or your script needs to check if the component is attached before using it.
UnityEngine.Camera.WorldToScreenPoint (Vector3 position)
DestroyerScript.Update () (at Assets/DestroyerScript.cs:27)
and this
$$anonymous$$issingComponentException: There is no 'Camera' attached to the "Patch Pirate(Clone)" game object, but a script is trying to access it.
You probably need to add a Camera to the game object "Patch Pirate(Clone)". Or your script needs to check if the component is attached before using it.
UnityEngine.Camera.WorldToScreenPoint (Vector3 position)
DestroyerScript.Update () (at Assets/DestroyerScript.cs:27)
and so on.
When I double click the in game exception errors, all of them lead to the 27th line in the destroyer script.
Any Ideas?
You probably need to add a Camera to the game object "Patch Pirate(Clone)". Well, do you?
It didn't work. When I ran the game, the screen just went blue, (from the camera I attached to the Patch Pirate). Should I put the camera attached to the Patch Pirate to the same position of the original camera? Or should I put the camera (Patch Pirate) in the same position of the Patch Pirate?
+Johat Yes, I want it so if I touch either side of the screen, it loads the next scene.
It has also worked so far, to where it loads the scene when i touch the LEFT side of the screen. Thank you guys so much!
Hi, glad to hear everything's working okay for you.
I updated the answer to also check the right side of the screen. It's no more complicated -- you just add in an extra check to see if the screen position is greater than the screeen width.
if (leftScreenPosition.x < 0f || rightScreenPosition > Screen.width) ERROR = error CS0019: Operator `>' cannot be applied to operands of type `UnityEngine.Vector3' and `int'
It is on line 43
Do you know what the problem is?
This game is 2D by the way,
Answer by Gaming-Dudester · Apr 19, 2016 at 01:33 AM
I don't fully understand but...try to make a gameobject that ALWAYS stays just outside the camera view. then use
void OnTriggerEnter(Collision coll)
{
if (coll.gameobject.tag == "boundsgameobject")
{
scenemanager.loadscene(gameover);
}
}
sorry if I misunderstood... I hope not tho. it would be better to show a pic of ur screen. is the game 2d or 3d? I don't fully understand what ur going for. hope thos helps.
Case matters - it's LoadScene(), which you can check easily in the API - http://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene.html
Also, be careful: though fixing the function call from "loadscene" to LoadScene will remove compiler errors, this still won't work.
That's because this isn't the correct signature for OnTriggerEnter (check here).
If you switch parameter type from Collision to Collider you should be okay though =).
EDIT:
Just reading again -- also not sure if you're using 2D or 3D Colliders? It makes a difference!
If you're using 2D Colliders, you need OnTriggerEnter2D with a parameter of type Collider2D.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075036im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unwanted rotation 0 Answers
Collision2D error when instantiating a prefab. 1 Answer
How to tell if a 2D object is in a certain area 2 Answers
Limiting camera to visible scene 0 Answers
set Edge Collider 2D to the borders of the camera's viewport 0 Answers