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.)