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 coolbird22 · Jun 07, 2014 at 08:10 PM · spritesbackground

How to change sprites based on score, with a dissolve ?

For every addition of 50 to the score, I want the background sprite to change to a different one by fading with a dissolve. Does it have to be something like an 'if' statement, for every 50, 100, 150, 200, etc score that I have to animate the alpha over time to make it look like a dissolve ? How is something like that achieved ?

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

Answer by sedativechunk · Jun 07, 2014 at 09:05 PM

You are correct, typically you want to access the alpha channel of the material of the object you are trying to fade in/out (or "dissolve" rather). I'm not sure this will work but your code might want to look something like this: // In your class, set up some variables to handle your scoring int oldScore, newScore; int multiplier = 2; bool fadeOut = false;

 void Update()
 {
 // let's assume you got some basic logic to determine when the score is increased
 // When someone does something to win points, track them in the new score
 newScore = oldScore;
 newScore += points;
 
 if (newScore >= (oldScore * multiplier))
     fadeOut = true;
 
 oldScore = newScore;
 }
 
 void FixedUpdate()
 {
 
 //  Get the starting color of your sprite
 Color currentColor = this.renderer.spriteRenderer.color.a;
 
 // Set up a speed variable to control how fast you want the fading to be
 float dissolveSpeed = 0.3f;
 
 // Fade the alpha on the new color variable we created
 // Even when  fading colors, it's a good idea to multiply them by delta time, there are other ways to accomplish this though
 // reset the fadeout when the alpha is zero
 if (fadeOut)
 {
     currentColor.a -= dissolveSpeed * Time.deltaTime;
     if (currentColor.a <= 0)
         fadeOut = false;
 }
 
 // Lastly set the color back to the new color with the alpha difference (you really only need to store a new variable if you are working in C# as you cannot directly modify the alpha in C#)
 this.rendered.spriteRenderer.color = currentColor;
 
 }


I hope that steers you in the right direction.

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

22 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

Related Questions

Stretching Background sprite 4 Answers

Unity 2D and Texture Memory. 1 Answer

Sprite Background Lags with Camera Script 0 Answers

Fastest way for tiled & Pixelated background 1 Answer

How to merge two background sprites so that there won't be a line between the merging sides? 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