Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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:
    5
    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:
    112
    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:
    5
    Thanks a lot that worked