Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by rgi92 · Feb 22, 2019 at 05:16 PM · scripting beginner

How do I improve my teleport script? (Screen freeze, delay, fade).

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);
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AaronBacon · Feb 22, 2019 at 07:30 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Floating scifi display to show results from database query 0 Answers

Having some doubts on the basics of scripting,A little doubt about some terminology 0 Answers

Adding prefab objects to a list on another script and object? 2 Answers

How do i find out if teleport was unsuccessful? (VR VIVE) 0 Answers

How do i make my enemies face the player? 2D 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges