• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Why is my coroutine not working? 0 Answers

What's the easiest way to fade a sprite in? - Looking for equiv of jQuery's fadeIn() 0 Answers

FadeIn/Out object composed of multiple sprites? 2 Answers

[Solved] Fading Two Materials Using Color 0 Answers

Screen Fader Problem 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges