Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Have a certain transparency based on distance?

Discussion in 'Scripting' started by dwhysall, May 11, 2018.

  1. dwhysall

    dwhysall

    Joined:
    Nov 22, 2012
    Posts:
    15
    Hi,

    This is really simple but I just keep on confusing myself and I'm not sure why. My brain is just saying no right now.

    What I want is that if my distance to a marker is 20m or less, then it's alpha is 0.8. Then past that, up to a distance of 50, I want the transparency to lerp smoothly between 0.8 and 0.2. Is it inverselerp? I dont know!

    Code (CSharp):
    1. float maxAlpha = 0.8f;
    2. float minAlpha = 0.2f;
    3. int minDistance = 20;
    4. int maxDistance = 50;
    5.  
    6. if ( distance <= minDistance )
    7. {
    8.     col.a = maxAlpha;
    9. }
    10. else
    11. {
    12.     // Code here
    13. }
    Any help would be massively appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Two steps:

    Step 1: calculate the "fraction" (third term of Lerp) from your near limit to far limit

    Code (csharp):
    1. float fraction = (distance - minDistance) / (maxDistance - minDistance);
    Step 2: use the fraction with Mathf.Lerp() to tween between your alphas:

    Code (csharp):
    1. col.a = Mathf.Lerp( maxAlpha, minAlpha, fraction);
    Lerp doesn't care about its direction-ness: it's happy to go up or down. The only thing it cares about is that third term's position between 0.0 and 1.0f.

    Same goes for Vector3.Lerp() coincidentally.

    Also don't be confused by the fact that some documentation actually calls that third term "alpha," hence why I use 'fraction' in the code above. I think the current docs call it 't' which is better.
     
  3. dwhysall

    dwhysall

    Joined:
    Nov 22, 2012
    Posts:
    15
    Perfect, thank you so much. I was trying it myself and I was getting a percentage for the lerp, but I was getting the wrong percent.

    Would you be able to explain why the fraction is done this way?
    Code (CSharp):
    1. float fraction = (distance - minDistance) / (maxDistance - minDistance);
    Don't worry if not - I just like to understand why stuff happens but my maths honestly isn't that great.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    The algebraic thinking process I used to create that expression is "I want the output to be zero at min distance."

    Therefore I subtract minDistance from distance. (Could become negative, but just keep reading).

    Then I think, "I want it to be 1.0 at maxDistance, BUT I have already subtracted minDistance from it."

    Therefore I must divide it by the span, which is maxDistance minus minDistance, which will make it 1.0 at maxDistance.

    Finally I am making use of the fact that this particular Lerp implementation clips below 0.0 and above 1.0. Other lerp implementations may extrapolate the lerp beyond the endpoint boundaries, which sorta makes them more of a "linear rescale" function than a linear interpolation (lerp) function.

    Hope that clarifies things a bit!
     
    FantasticGlass likes this.
  5. dwhysall

    dwhysall

    Joined:
    Nov 22, 2012
    Posts:
    15
    Thank you very much!
     
  6. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    Thanks Kurt, this will definitely help me in the future. :)
     
    Kurt-Dekker likes this.