Door Between Scenes

Hey guys, I’m pretty new to Unity and I have a simple question that I couldn’t find an answer to. You see, I have a door model in my 3D project, and I want the player to be able to press Space on it to then fade to black and fade out into another scene. How would I do something like this?

Raycast or use a trigger collider to check if the player is next to the door.

If they are, load your scene. You can just place a canvas and panel over the entire screen and fade that to black, load your next scene, have a canvas and panel again in the next scene which is black to start with and fade that to a 0 alpha when the scene starts.

So, your question is split into 2 main parts:

  • How do you detect the player is looking at an object and pressing Space
  • How do you fade to black and then swap scene

In the future, you should break questions down into parts like this, as you are far more likely to be able to google these more basic questions Anyway, here’s how you do your issue:


Part 1: Look and Input Detection

To test if a player is looking at the door, you will want to first do 2 things to the door. First, add a Tag to it using the Tag area of the Inspector. You probably want to create a new tag and call it “Door” or similar 133839-untitled.png
Then, you need to add a Box Collider to the door, to allow a Script to detect what area should be defined as “looking at the door”. After that, you need to make a new script component on your player called DoorInteractor or something sensible, and then create a Raycast that detects when the door is in front of you. Raycasts essentially draw a straight line from one point to another, and note what they hit along the way. in practice, this is what it looks like:

using System.Collections.Generic;
using UnityEngine;

public class DoorInteractor : MonoBehaviour
{

    // Sets a Max Distance the door can be seen from.
    float maxDistance = 10;
    static public bool hitDoor = false;
    // Allows the Door Script to know if it's being Looked at right now

    void FixedUpdate()
    {

        // Will contain the information of which object the raycast hit
        RaycastHit hit;


        // Sends out a Raycast, if it hits something, is that something Tagged as a "Door" in the Editor
        if (Physics.Raycast(transform.position, transform.forward, out hit, maxDistance) && hit.collider.gameObject.CompareTag("Door"))
        {
            hitDoor = true;
        }
        else
        {
            hitDoor = false;
        }
    }
}

so now you have a script that can detect if you look at the door, but you also need it to detect if the player presses space. this part is easy, as unity has an input module to do just that:

        if (Physics.Raycast(transform.position, transform.forward, out hit, maxDistance) && hit.collider.gameObject.CompareTag("Door"))
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                hitDoor = true;
            }
        }

So now we have a script that can detect if the player presses space while looking at the door.

Now we need the effects of that space press to be acted out.

Part 2: Fade To Black and Scene Change

This part involves the Unity UI. The Easiest way to make a Fade to Black over the screen is to just create a giant black square over the screen, and then change its opacity using scripts. To do this create a new UI Image in the scene.

133841-untitled2.png

Now, you should notice it creates with it a Canvas Object. On that Canvas, add a Component called CanvasGroup. CanvasGroup is useful as it has a handy Opacity value, useful for a fade to black transition. Now, the first thing to do is to make the Image a Giant Screen Covering Black Square, which can be done pretty easy in the inspector. Then, you want to make a new script on the Door this time, and make it have a link to this canvas (You can just set it in the inspector) This should then gradually change the canvas Alpha value (Opacity) and then Swap scenes, using UnityEngine.SceneManagement.

Here’s the Script on the Door:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class DoorTest : MonoBehaviour
{

    public CanvasGroup overlay; 
    // Set this to the Canvas Object in the Editor
    public float transitionSpeed; 
    // May want to slow down the transition, set this to below 1 to slow
    private bool warping = false;

    private void Awake()
    {
        overlay.alpha = 0;
    }
    void Update()
    {
        if (DoorInteractor.hitDoor == true || warping == true) 
            // If the Player has set the Door Trigger or you are already warping
        {
            warping = true;// Note you are now warping
            overlay.alpha = overlay.alpha + Time.deltaTime*transitionSpeed; // Gradually change the Opacity each time this is run
        }
        if (overlay.alpha >= 1) // if its fully opaque
        {
            SceneManager.LoadScene("myScene1"); // Change Scene
        }
    }

}

And that should be it, If you have any issues, just let me know and I’ll try to adress them.

We made a tutorial on how to change scenes and end up in the right spot on our channel. Using this method you can have as many doors in as many or as few scenes as you want.

Hope this helps!