Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question How do i make it so the camera kills the player if they go off screen

Discussion in '2D' started by HarveyF03, Mar 26, 2023.

  1. HarveyF03

    HarveyF03

    Joined:
    Oct 12, 2019
    Posts:
    4
    Im making a 2d endless runner where the player has to run down away from the camera, how would I make it so when the player goes off the camera it kills them and sends them back to the main menu. The game is like similar to a reverse doodle jump
     
  2. Autoface

    Autoface

    Joined:
    Sep 23, 2013
    Posts:
    101
    An easy way for detecting the player going off screen would be using OnBecameInvisible();

    Try attaching this script to the player gameobject:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MainMenuWhenOffScreen_Script : MonoBehaviour
    7. {
    8.     void OnBecameInvisible()
    9.     {
    10.         SceneManager.LoadScene("SceneName");
    11.     }
    12. }
    Replace "SceneName" with the name of yoru scene
     
  3. HarveyF03

    HarveyF03

    Joined:
    Oct 12, 2019
    Posts:
    4
    Thanks a lot that worked