Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Conect11 · Sep 06, 2013 at 06:05 PM · fade3dtext

Attempting to fade 3d text

Firstly: I know this has been documented, and answered before, as evidenced here among numerous other places. The issue isn't a lack of help, it's my complete newness at coding. I'm attempting to code a fade in and fade out for 3d text. I've placed the code from here into an empty game object in my scene, and am attempting to attach the following code into the 3d text:

 Fade.use.Alpha(GetComponent(TextMesh).renderer.material, 0.0, 1.0, 2.0);
 {
 
 
 function ()
 {
 for(i=1;i>0;i++)
 {
 yield WaitForSeconds(3.0);
 }
 }

Entering into this, I knew I would code something wrong, as my entire coding education so far has been stumbling along until something works, and then asking for help, and hoping that the advice not only fixes the current issue, but that I retain why the advise worked. So I guess I'm asking if someone can tell this noob what I need to add to my script to actually make it, you know, a script. Thanks, and God bless.

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
2
Best Answer

Answer by clunk47 · Sep 06, 2013 at 07:42 PM

Here's an example I came up with for your question. Simply attach this to your 3DText Object, then use the 'E' key to toggle the fade. I made this only fade out if alpha is 1.0, and only fade in if alpha is 0.0. There are many different ways of doing this but hopefully this will get you going.

Here's a C# Example

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     bool fadeIn = false;
     bool fadeOut = false;
     float fadeSpeed = 0.01f;
     float minAlpha = 0.0f;
     float maxAlpha = 1.0f;
     Color color;
     
     void Awake()
     {
         color = renderer.material.color;    
     }
     
     void Update()
     {    
         renderer.material.color = color;
         
         if(fadeIn && !fadeOut)
             FadeIn ();
         
         if(fadeOut && !fadeIn)
             FadeOut ();
         
         if(color.a <= minAlpha)
         {
             fadeOut = false;
             if(Input.GetKeyDown (KeyCode.E))
             {
                 fadeIn = true;    
             }
         }
         
         if(color.a >= maxAlpha)
         {
             fadeIn = false;
             if(Input.GetKeyDown (KeyCode.E))
             {
                 fadeOut = true;    
             }
         }
     }
     
     void FadeIn()
     {
         color.a += fadeSpeed;
     }
     
     void FadeOut()
     {
         color.a -= fadeSpeed;
     }
 }


And here is a Javascript example of the same thing.

 #pragma strict
 
 var fadeIn : boolean;
 var fadeOut : boolean;
 var fadeSpeed : float = 0.01;
 var minAlpha : float = 0.0;
 var maxAlpha : float = 1.0;
 
 function Update()
 {      
     if(fadeIn && !fadeOut)
         FadeIn ();
     
     if(fadeOut && !fadeIn)
         FadeOut ();
     
     if(renderer.material.color.a <= minAlpha)
     {
         fadeOut = false;
         if(Input.GetKeyDown (KeyCode.E))
         {
             fadeIn = true;  
         }
     }
 
     if(renderer.material.color.a >= maxAlpha)
     {
         fadeIn = false;
         if(Input.GetKeyDown (KeyCode.E))
         {
             fadeOut = true; 
         }
     }
 }
 
 function FadeIn()
 {
     renderer.material.color.a += fadeSpeed;
 }
 
 function FadeOut()
 {
     renderer.material.color.a -= fadeSpeed;
 }
 
Comment
Add comment · Show 8 · 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 clunk47 · Sep 06, 2013 at 08:16 PM 1
Share

You did create a C-Sharp script, and not a JavaScript right?

avatar image Conect11 · Sep 06, 2013 at 08:26 PM 2
Share

no worries, lol. I had just been used to using js, but see no reason not to try and learn on either. Although oddly enough, while I get no compile errors, and the script shows up in my assets, it doesn't show up as an available component to add. Sorry, I know this is all stuff that is probably so basic.

Other question is this: if I simply want this to fade in when the player gets in range, and then fade out on its own (it's hovering text over an enemy, think like the boss fights in Ocarina of Time) would I just remove the "get$$anonymous$$eyDown - E" sections?

Edit: You are absolutely incredible. Thank you! This definitely helped me a ton. Next steps for me are trying to get the text to fade without the key press, and incorporating a pause before the battle begins. Thank you SOOOOO much for getting me to this point :)

avatar image Conect11 · Sep 06, 2013 at 09:56 PM 2
Share

Yes Yes Yes!!!!!!! Praise God! =) Using Clunk's fantastic js script, and principles learned through Jesse Etzler's tutorials, I managed to get the text to appear when the player is a certain distance, and then fade out again at a closer distance. Here's the modified script: (ps: sorry for double posting, didn't seem to fit the above comment)

 #pragma strict
      
     var fadeIn : boolean;
     var fadeOut : boolean;
     var fadeSpeed : float = 0.01;
     var $$anonymous$$Alpha : float = 0.0;
     var maxAlpha : float = 1.0;
     var $$anonymous$$Distance = 30;
     var target : Transform;
     var myTransform : Transform;
     
     function Awake()
     {
     myTransform = transform;
 }
 
     function Start()
     {
     
     target = GameObject.FindWithTag("Player").transform;
     
     }
      
     function Update()
     {
     
     if(fadeIn && !fadeOut)
     FadeIn ();
      
     if(fadeOut && !fadeIn)
     FadeOut ();
      
     if(renderer.material.color.a <= $$anonymous$$Alpha)
     {
     fadeOut = false;
     if(Vector3.Distance(myTransform.position, target.position) > $$anonymous$$Distance)
     {
     fadeIn = true;
     }
     }
      
     if(renderer.material.color.a >= maxAlpha)
     {
     fadeIn = false;
     if(Vector3.Distance(myTransform.position, target.position) < $$anonymous$$Distance)
     {
     fadeOut = true;
     }
     }
     }
      
     function FadeIn()
     {
     renderer.material.color.a += fadeSpeed;
     }
      
     function FadeOut()
     {
     renderer.material.color.a -= fadeSpeed;
     }
avatar image clunk47 · Sep 06, 2013 at 11:11 PM 1
Share

Looks like you're learning fast, I'm always happy to help someone so dedicated to beco$$anonymous$$g a good coder. Happy Developing Sir :D

avatar image Conect11 · Sep 06, 2013 at 11:19 PM 1
Share

thanks! Did find a hiccup though: text has actually appeared far earlier than I'd like, plus is showing through walls, buildings, you name it. Definitely fades when I get close, but have to figure out how to not have it show up so soon, since it's the boss' text, and would kind of give away its position :)

EDIT: Hmm, well, attempt 1 at that edit didn't work. $$anonymous$$y theory was to make a new variable: "$$anonymous$$Distance2" and set it up a bit higher than $$anonymous$$Distance, then copy paste the if functions, changing their values. No dice, the text will fade out, but is still always there. So the question is how to make it not fade in until X distance away. Back to the lab again.

Show more comments

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Fade in object using iTween 0 Answers

Collision based scoring system 1 Answer

Script attached to collider being activated when player hits different collider 1 Answer

How to access a static variable in c# from js 2 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