- Home /
How to make an exit level trigger?
I want to make a platform that when walked on exits the level and brings you back to a level selection screen. Any thoughts/resources anyone has?
Answer by betaFlux · Dec 30, 2018 at 11:54 PM
I tend to immitate techniques of famous games. In this case I would do it like Neverwinter Nights did it.
Add an empty gameobject to the scene
Attach a box collider to it, set it to trigger and adjust its dimensions to your liking
Now attach a script with the following code to the trigger object:
public class Trigger : MonoBehaviour { public string sceneName; void OnTriggerEnter(Collider c) { if(c.tag == "Player") UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName); } }
If your player's tag is "Player" and has a collider attached, it should work. Make sure to type the correct scene name into the Trigger Script inspector.
Answer by triis4924 · Dec 30, 2018 at 11:44 PM
just make an empty object with is trigger checked then make a script that looks like this:
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "player")
{
SceneManager.LoadScene(sceneyouwanttoload);
}
}
make sure you write using UnityEngine.SceneManagement; and the player has a player tag
Answer by bitthebillias · Dec 31, 2018 at 12:08 AM
okay cool! that worked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Trigger : MonoBehaviour
{
public string sceneName;
void OnTriggerEnter(Collider c)
{
if (c.tag == "Player")
UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
}
/*public void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.tag == "player")
{
SceneManager.LoadScene("MainMenu");
}
}
*/
}
so now i ran into another problem after that:
I am using the unity first person camera in this project and when i jump back into the "mainMenu" scene, i dont have control of the mouse to select anything... Thoughts?
In your current Trigger script you may remove the variable sceneName if you directly load scene with "$$anonymous$$ain$$anonymous$$enu". Also as you added the namespaces Scene$$anonymous$$anager.LoadScene("$$anonymous$$ain$$anonymous$$enu");
is enough.
As for your problem, are you sure that the camera exists in both scenes?
it doesn't exist in both. i want to be able to enter the level through a level button menu, go play through the level. Exit. Then be sent back into the level button menu to pick the next level.
If i could regain mouse control from the camera, i might be able to get somewhere.
Eventually i want to have the next level to be unlocked by exiting the previous level.
In the level itself is the cursor hidden? If so, when entering the level selection screen, you could either disable the script that hides and locks the cursor or create a little extra script which enables the cursor again: Cursor.lockState = CursorLock$$anonymous$$ode.None; Cursor.visible = true;
Your answer
Follow this Question
Related Questions
Problems with unlocking levels 1 Answer
How to manage multiple levels in the 2D game ? 3 Answers
Level Selector not working on build 0 Answers
How to load scenes based on specific percentage? 0 Answers
manage objects through a manager script 0 Answers