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 Juvut · Nov 14, 2014 at 01:49 PM · spritefadefadeoutfadein

Fade out sprite on click

Hello! I want to fade out sprite when I click on it.

So far I have:

     public SpriteRenderer sprite;
     public float minimum = 1f;
     public float maximum = 0f;
     public float duration = 5f;

and

     float t = Mathf.SmoothStep(minimum,maximum, Time.time / duration);
     sprite.color = new Color (1f, 1f, 1f, t);

^attached to click on the sprite. Everything is ok if I click on it instantly after I run my application. But if I wait some time and then click it - it disappears instantly.

How can I fix it?

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 AlwaysSunny · Nov 14, 2014 at 10:43 AM 0
Share

There are many ways to script time-based events. Your issue arises because Time.time accumulates. You should research coroutines and Time.deltaTime.

4 Replies

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

Answer by Wisearn · Nov 14, 2014 at 02:52 PM

I would suggest making a Coroutine for the purpose of "animated" code, like a fading.

See: http://docs.unity3d.com/Manual/Coroutines.html for not only info about coroutines but also code to alpha fade just like you want it (you will need to make some changes based on the context of your game and game mechanics).

Comment
Add comment · Show 1 · 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 Juvut · Nov 15, 2014 at 09:23 PM 0
Share

Thank you! That was the best idea here and it worked like a charm. :)

avatar image
0

Answer by Eck · Nov 14, 2014 at 02:59 PM

It's because you are using the Variable Time.time, which is how long the game has been running since the start. When you wait 5 seconds before clicking, the fade out sequence is already 5 seconds into it. You can solve this a few ways.

 // Member variable to hold the time when the user clicked.
 private float clickStartTime;
 
 
 // In your clicked function, store the time:
 clickStartTime = Time.time;
 
 
 // In your fade out function, take the difference
 elapsedTime = Time.time - clickStartTime;
 float t = Mathf.SmoothStep(minimum,maximum, elapsedTime / duration);

Alternatively, you can use Time.deltaTime and accumulate the elapsedSeconds in a variable, and just use that.

  • Eck

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 Juvut · Nov 15, 2014 at 09:28 PM 0
Share

Thank you! I think @Wisearn had a better idea but thank you too. :)

avatar image Eck · Nov 16, 2014 at 08:22 PM 0
Share

Wow, that co-routine stuff is pretty slick. I've only been messing with Unity for a week or two so I hadn't stumbled across this technique yet. Very cool.

  • Eck

avatar image
0

Answer by SmalleeStudio · Nov 14, 2014 at 02:54 PM

Time.time means The time "since the start of the game".

So when the time of the game goes 5 second, the vaiue : Time.time / duration = 1

It may disappear instantly

Comment
Add comment · Show 1 · 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 Juvut · Nov 15, 2014 at 09:27 PM 0
Share

Thank you! It is clear to me now. :)

avatar image
0

Answer by PixelFireXY · Nov 14, 2014 at 03:01 PM

What do you need is to follow some unity tutorials to learn the basics of coding in unity.

What do you need for your code is this video: http://unity3d.com/learn/tutorials/modules/beginner/scripting/lerp

and what do you need first of all is to follow all these tutorials: http://unity3d.com/learn/tutorials/modules/beginner/scripting

good coding ;)

Comment
Add comment · Show 1 · 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 Juvut · Nov 15, 2014 at 09:27 PM 0
Share

Thank you! :) I will try to read all these.

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

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Fade in/out a GameObject 1 Answer

Smoothly Fade in a texture 1 Answer

How do I make the screen fade when it goes into the next level? 0 Answers

Fade In / Out UI Image 3 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