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 youngapprentice · Jul 18, 2012 at 08:52 PM · mathfade

Quick change over time math

Hello, all! I am writing a script that allows you to set parameters for the time you would like a guitexture to stay dormant, then fade in, then stay constant, then fade out.

I would just like to know how to calculate this:

After the number of dormant seconds has been reached, I would like to calculate the rate at which an object needs to fade in and how to make a fluid transition independent of frames.

For instance, if the FadeinTime variable is set to 4.0 seconds, I would like to take the guitexture's alpha from 0 to 100 in 4.0 seconds, but instead of updating every second, I'd like to update it as quickly as possible at a set rate.

How would I do this?

Thanks for the help! (And I will release the script when I finish) - YA

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

2 Replies

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

Answer by Bovine · Jul 18, 2012 at 09:15 PM

You should Lerp the colour in Update().

See Unity's example here:

http://docs.unity3d.com/Documentation/ScriptReference/Color.Lerp.html

You will need to provide your own time value based on simple maths for how long has elapsed since the fade began.

Note you could also do this with a Coroutine, which is probably a better choice, so you don't Update() unnecessarily:

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

Comment
Add comment · Show 4 · 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 youngapprentice · Jul 19, 2012 at 04:53 PM 0
Share

Quick question though: How do I use the 't' field in the $$anonymous$$athf.Lerp? If I want it to go from an alpha of zero to an alpha of 1 in 3 seconds, I cannot type in alpha = $$anonymous$$athf.Lerp(0.0,1.0,3); That does not work...

avatar image Bovine · Jul 19, 2012 at 06:06 PM 0
Share

T is normalised time so you need to say from 0 to 1 how far through the Lerp operation you are. This would be best done in a coroutine if it is a one shot kind of thing.

avatar image youngapprentice · Jul 19, 2012 at 06:27 PM 0
Share

Hmm... I already have it in a coroutine. So what value would I insert for 3 seconds? 0.3333?

avatar image Bovine · Jul 20, 2012 at 09:59 AM 0
Share

Something like:

IEnumerator Fade(material mat, float duration, Color start, Color end) { // remember the start float start = Time.time; do { // calculate how far through we are float elapsed = Time.time - start; float normalisedTime = $$anonymous$$athf.Clamp(elapsed / duration, 0, 1); mat.color = Color.Lerp(start, end, normalisedTime); // wait for the next frame yield return null; } while(elapsed < duration); }

I've not compiled it, but it should be fine.

avatar image
0

Answer by Bovine · Jul 20, 2012 at 10:01 AM

Something like:

 IEnumerator Fade(material mat, float duration, Color start, Color end)
 {  // remember the start
    float start = Time.time;
    do
    {  // calculate how far through we are
       float elapsed = Time.time - start;
       float normalisedTime = Mathf.Clamp(elapsed / duration, 0, 1);
       mat.color = Color.Lerp(start, end, normalisedTime);
       // wait for the next frame
       yield return null;
    }
    while(elapsed &lt; duration);
 }

I've not compiled it, but it should be fine.

Comment
Add comment · Show 5 · 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 youngapprentice · Jul 20, 2012 at 03:28 PM 0
Share

Thank you so much. I have been coding for a while but I guess I'm really not as proficient at this as I thought I was xD Alright just two more questions: What is IEnumerator? Are you just passing in variables? And what is the while statement for? And is "do" actually necessary for a coroutine? I'm sorry that I am bombarding you with questions but I'd like to pick something up from this rather than just copy-paste code. Thanks so much! -YA

avatar image Bovine · Jul 20, 2012 at 04:23 PM 0
Share

Good grief! There are a raft of questions ranging from relatively simple program$$anonymous$$g issues to quite big ones.

Coroutines in Unity allow you to yield execution of the next part of the coroutine until some future time - the next frame, fixed update time or a set period of time, via WaitForSeconds().

This mean that once you StartCoroutine() that your Coroutine will run until you yield and will potentially be run again the following frame and so on until you yield break or the coroutine returns (you must yield at least once for some reason).

Coroutines in Unity are documented here:

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

IEnumerator is an enumerator - the coroutine is treated as an enumeration, but this is typically not something you will care about.

Enumerators are described here however:

http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx

do, while is a general program$$anonymous$$g construct. The syntax may differ from language to language, but in c# you need to begin a do, while with do. A while, do you do not need to end with do - the ter$$anonymous$$ating scope brace will do this for you. This construct does not change just because we are in a coroutine.

You would use the Coroutine above by calling:

StartCoroutine(someRenderer.material, 3, Color.white, Color.black);

Your coroutine would begin, perhaps at that point, I don't know (though I have never checked) and the function would execute normally until it hit the yield return null. At which point the Coroutine would return, technically yield and execution would return to the gameloop which would eventually finish processing that frame.

When the next frame is executed, at some point, because you yield return null, the coroutine will pickup its execution at the instruction following the one it last yielded at. So your routine would check the elapsed time calculate wasn't more or the same as the duration and if it wasn't it would execute from the do again, and yield once more and so on and so forth.

Coroutines are state machines. Ins$$anonymous$$d of you having to remember what you were doing using some sort of switch statement or series of nested switch statements, the framework takes care of it for you.

$$anonymous$$ore detail can be found in this answer:

http://answers.unity3d.com/questions/36690/mechanics-of-coroutines.html

I can't rally help much more, but do search the internet and read around the topics mentioned.

Note that you can also fade a material's color using an animation.

avatar image youngapprentice · Jul 20, 2012 at 04:38 PM 0
Share

Oh yes I am not that much of a newbie to know about animation haha. While I have worked with 'while' statements, never have I worked with a do-while. Doesn't sound all too complicated. Thank you very much this clears up a lot. Very glad you found this thread haha. Thanks for the help! -YA

avatar image youngapprentice · Jul 20, 2012 at 05:24 PM 0
Share

Oh my gosh I really do apologize but I am working in Javascript not C#... And it appears IEnumerator interface is C#...?

avatar image Bovine · Jul 20, 2012 at 05:59 PM 0
Share

Read the docs please, there are equivalents of all of the above in JS and the docs contain examples. I've written the routine for you in C# - I can't really give you anymore help.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I Fix Unexpected Char 0x201C? (Code Below) 1 Answer

i need some code suggestions 1 Answer

Wall Jump script 2 Answers

AssetBundle Android 1 Answer

Error BCE0018 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