Hello. I have a script that I use for teleport, but I wanted to improve it but I don’t know how because I am just a beginner in C#.
Things I want to do, is when I press a button while on trigger, my screen will freeze, sound will play and fade out into black, and then fade in on the destination
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class TeleportWithinScene1 : MonoBehaviour
{
public GameObject ui;
public GameObject target;
public Transform destination;
public AudioSource audioSource;
public AudioClip teleportSFX;
void Start()
{
ui.SetActive(false);
}
void OnTriggerStay(Collider other)
{
ui.SetActive(true);
if ((other.gameObject.tag == "Player") && Input.GetButtonDown("Enter"))
{
target.transform.position = destination.transform.position;
target.transform.rotation = destination.transform.rotation;
audioSource.PlayOneShot(teleportSFX);
FirstPersonController fpc = target.gameObject.GetComponent<FirstPersonController>();
fpc.SetRotation(destination.transform);
}
}
void OnTriggerExit()
{
ui.SetActive(false);
}
}
Well, the way I did this was to use a Canvas group that contains a black square overlaid over the entire screen. Canvas groups can have their opacity edited easily via script, making it quite easy to get a fade effect by simple using time.deltaTime to add or subtract from the opacity. Heres how I did it in a script used to teleport the player to a new scene via a door:
public GameObject player; // Provides a link to the Players character
public GameObject interactSign; // Provides a link to the "E" Interact prompt
public CanvasGroup overlay; // Stores the Screen fade Overlay
public AudioClip warpSound; // Stores the Warping sound
public string goToLevel; // Stores which Scene this door is assigned to go to
private bool inRange; // Used to store if you're in range of the Door or not
private bool warping; // Stores if you are warping to a new level, used for the animation.
void OnTriggerEnter2D(Collider2D player) // when the player overlaps with the Doors Collision Box
{
inRange = true; // Note the player is in range of the Door
if (OptionsController.showInteracts == true)
{
interactSign.gameObject.SetActive(true); // Show the "E" Interact Prompt
}
}
void OnTriggerExit2D(Collider2D player) // When the player leaves the Doors Collision Box
{
inRange = false; // Note the player is out of range of the Door
if (OptionsController.showInteracts == true)
{
interactSign.gameObject.SetActive(false); // Hide the "E" Interact Prompt
}
}
void Update()
{
if (Input.GetKeyDown("e") || Input.GetKeyDown("joystick button 0")) // If you press E or "A" on a Controller:
{
if (PlayerController.frozen == false) // Check the Player isn't frozen
{
if (inRange == true) // And you are in range of the Door
{
warping = true; // Begin the warping Animation
AudioSource.PlayClipAtPoint(warpSound, Vector2.zero);
PlayerController.frozen = true; // freeze the player (Done in the players script)
}
}
}
if (warping == true) // When the warping trigger is set
{
overlay.alpha = (overlay.alpha + (Time.deltaTime * 3)); // Slowly Changes the Overlay Opacity to create a fade effect.
if (overlay.alpha == 1f) // when the screen is fully Black
{
PlayerController.frozen = false; // Unfreeze the player
SceneManager.LoadScene(goToLevel); // Go to the Level specified in the Editor
}
}
}
Hopefully you can take what you need from this and apply it to make a fade effect, it’s mostly what you need, albeit a few extras like an interact prompt that I have when you walk in range of a door.