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);
         }
 
              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));
 
              That worked perfectly, thank you :) For anyone who might use this, I changed the multiplier to .3f for a smoother transition.
Your answer
 
             Follow this Question
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