Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by FlyingFlambe · Oct 22, 2016 at 08:32 PM · coloruser interfaceraycasthit2dgradient

How to reverse color gradient effect

Hi, I'm currently dealing with something that's mostly figured out, but the opposite of what I want is occurring.

What I'm trying to do is that when an object above the player is approaching more closely, the screen gets darker for a claustrophobic feel. What's happening instead is that when the object gets further away, the screen gets darker; and if the object gets closer, the screen gets brighter. How would I reverse this effect?

Also, sorry if the code isn't elegant; still learning C#. Thank you!

 void Crushed()
     {
         ///Create a screen-darken effect around the borders if the player is close to being crushed.
         
         RaycastHit2D hitUpRange = new RaycastHit2D();
         hitUpRange = Physics2D.Raycast(transform.position, Vector2.up, crushRangeMax, whatIsHexPlate);
 
         RaycastHit2D hitDownRange = new RaycastHit2D();
         hitDownRange = Physics2D.Raycast(transform.position, Vector2.down, crushRangeMax, whatIsHexPlate);
 
         if ((hitUpRange.collider != null && hitUpRange.collider.tag == "HexPlate") && (hitDownRange.collider != null && hitDownRange.collider.tag == "HexPlate"))
         {
             isInCrushRange = true;
 
             other = hitUpRange.collider.transform;
             float crushRangeDistance = Vector2.Distance(other.position, transform.position);
             gradientColor = new Color(0f, 0f, 0f, (crushRangeDistance * .1f));
         }
         else
         {
             isInCrushRange = false;
         }
 
         if (isInCrushRange)
         {
             crushImage.color = gradientColor;
         }
         else
         {
             crushImage.color = new Color(0f, 0f, 0f, 0f);
         }
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
1

Answer by conman79 · Oct 22, 2016 at 10:11 PM

On this line you are decreasing the alpha value as the object gets closer.

 gradientColor = new Color(0f, 0f, 0f, (crushRangeDistance * .1f));

0 alpha is completely transparent and 1 is completely opaque, so we gotta go the other way around by subtracting the distance from 1.

 gradientColor = new Color(0f, 0f, 0f, 1f - (crushRangeDistance * .1f));

I'm not sure if negative alpha values will throw up errors (haven't tested that), so just in case add a Mathf.Clamp() to keep anything negative as 0f. Like this:

 gradientColor = new Color(0f, 0f, 0f, Mathf.Clamp(1f - (crushRangeDistance * .1f), 0f, 1f));


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 FlyingFlambe · Oct 24, 2016 at 05:31 PM 0
Share

That worked perfectly, thank you :) For anyone who might use this, I changed the multiplier to .3f for a smoother transition.

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

76 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 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

blocks coloring like in game The Stack? 1 Answer

Gradients in 2D 0 Answers

How do I get my Buttons to switch colors? 0 Answers

Change button color to intermediate value on onclick() event 1 Answer

Unity - ScreenSize resolution problem 1 Answer


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