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 bathorsthroat · Jan 14, 2016 at 12:08 PM · fadeoutfadein

I need help getting a camera to fade up from black after my player teleports to next room position.

Here's my script, I need the game to fade from clear to black when I press the E key. Then fade from black to clear once my player changes transform positions. Any ideas? thanks!

 #pragma strict
 
 var fPlayer : Transform; 
 var doorSound : AudioClip;
 var nextRoom : Transform;
 var doorSoundPlay : boolean = false;
 
 function Start () 
 {
     
 }
 
 function OnTriggerStay(Col : Collider)
     {
         if(Col.tag == "Player")
         {
             if(Input.GetKey(KeyCode.E))
             {
                 fPlayer.transform.position = nextRoom.position;
                 GetComponent.<AudioSource>().PlayOneShot(doorSound);
                 doorSoundPlay = true;
             }
         }
     }
 
 function OnTriggerExit(Col : Collider)
     {
         if(Col.tag == "Player")
         {
             doorSoundPlay = false;
         }
     }
 
 function Update ()
     {
 
     
     }

  
Comment
Add comment · Show 1
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
avatar image corn · Jan 14, 2016 at 02:28 PM 0
Share

You could use a UI Image with no sprite, just black color, and when you hit E, start a coroutine that fades its alpha from 0 to 1.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toddisarockstar · Jan 14, 2016 at 09:12 PM

 var col:Color;
 col=Color(0,0,0,0);
 
 var tx:Texture2D;
 tx= new Texture2D(1,1);
 
 var fadeblack:boolean;
 
 function OnGUI () {
 GUI.color=col;
 GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),tx);}
 
 function Update(){
 if(fadeblack) {if(col.a<1){col.a=col.a+Time.deltaTime;}}
 if(!fadeblack){if(col.a>0){col.a=col.a-Time.deltaTime;}}
 
 if(Input.GetKeyDown("e")){fadeblack=!fadeblack;}
 }

put this script in your game somewhere and push the "e" key. Customize it as you need.

Comment
Add comment · Show 2 · 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
avatar image bathorsthroat · Jan 14, 2016 at 11:30 PM 0
Share

I don't this will work because is you look at my script I need the fade to black to happen after "fPlayer.transform.position = nextRoom.position;" and I can't call a guifunction inside "function OnTriggerStay" and I can't call it after trigger stay either. :(

avatar image toddisarockstar · Jan 15, 2016 at 05:57 AM 0
Share

this code is tested! and it works to fade in and out. it was an example how to fade. in this code the only thing you need to do is change my fadeblack variable

if you add all this to your script. simple make fadeblack=true to fade to black and make fadeblack=false to fade back in!!! change the one variable anywhere you would like in your code.

the the last line i wrote asking for "e" simply toggles fadeblack to true and false. for example purposes.

so just make my last line say fadeblack=true when button is pushed.

when your trigger happens just say fadeblack=false;

avatar image
0

Answer by bathorsthroat · Jan 15, 2016 at 02:32 AM

BUMP

Ok I got it to fade but it will only fade the first time it is triggered, I need is to trigger the fade every time "fPlayer.transform.position = nextRoom.position" is called. Here's my script??

 var fPlayer : Transform; 
 var doorSound : AudioClip;
 var nextRoom : Transform;
 var doorSoundPlay : boolean = false;
 var fadeTime : float = 5;
 var fadeTexture : Texture;
 var fadeOut : boolean = false;
 var tempTime : float;
 var time : float = 0.0;
 var fadeOutSound : AudioClip;
 var camera :GameObject;
 
 
 
 
 
 
 function Start () 
 {
     fadeOut = false;
     GetComponent.<AudioSource>().clip = fadeOutSound;
     GetComponent.<AudioSource>().Play();
    
     
 }
 
 function OnTriggerStay(Col : Collider)
     {
         if(Col.tag == "Player")
         {
             
                   
             if(Input.GetKey(KeyCode.E))
             {
                 fadeOut = true;
                 fPlayer.transform.position = nextRoom.position;
                 GetComponent.<AudioSource>().PlayOneShot(doorSound);
                 doorSoundPlay = true;
                 
                 
             }
         }
     }
      
     function OnTriggerExit(Col : Collider)
     {
         if(Col.tag == "Player")
         {
             
             doorSoundPlay = false;
 
             }
     }
 
     function Update ()
     {
         if (fadeOut){
             if(time < fadeTime) time += Time.deltaTime;
             tempTime = Mathf.InverseLerp(0.0, fadeTime, time);
         }
  
         if(tempTime >= 1.0) enabled = false;
     }
 
     function OnGUI(){
         if(fadeOut){
             GUI.color.a = 1 - tempTime;
             GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
         }
     }


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

38 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

Related Questions

How to fade in and out two logos inside the same scene? [c#] 1 Answer

Animation Not Playing 0 Answers

How to fade in and out two panels in a scene? (c#) 1 Answer

How to fade in and out a image in a loop?,How do I fade in and out image on a loop? 0 Answers

Audio Fade In / Fade Out with Trigger 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