- Home /

# interpolate color depending on distance from planet

hello everyone, I want to make it so the cameras bacground color changed gradualy as I got further or closer to the planet. i decided to go with color.Lerp that allows me to interpolate between 2 colors based on an number between 0 and 1. I have tried using linear functions but it is not getting me anywhere. here is what i`ve got so far:

```
//set atmosphere
if (closestPlanet != null){
float plntDist = Vector3.Distance (transform.position, closestPlanet.transform.position);
float planetRadius = (closestPlanet.transform.localScale.x/2);
float yAxisPoint = -(planetRadius/(planetRadius - planetRadius/3));
if (plntDist < planetRadius + planetRadius / 3){
colInterpolationT = (1/(planetRadius - planetRadius / 3)) * plntDist + yAxisPoint; // t$$anonymous$$s is an attempt of a linear function (explained at the end)
}
else colInterpolationT = 0;
currSkyColor = Color.Lerp(PossibleSkyColor[1], PossibleSkyColor[2], colInterpolationT);
cam.backgroundColor = currSkyColor;
}
```

any ideas or suggestions are very much apreciated. thank you all.

the linear function worked the following way

i used 2 points to get the formula: (0, planetRadius)(1, atmosphere radius) (atmosphere radius is a tird of the planets radius btw)

out of t$$anonymous$$s i got m = 1/(r - atmos) = 1 / ( planetRadius + planetRadius/3 - planetRadius)

finaly b = 0-planetRadius/ planetRadius -atmosphere radius)

### People who like this

But what is the problem?

Out of nothing, I would take the max distance to the planet and the min distance. With this, I can normalize the position and use it in the lerp.

Is there a maximum distance you can be from the planet? What's not working for you? Are you getting errors?! `PossibleSkyColor[1], PossibleSkyColor[2]`

<- are you sure this is correct, arrays are normally 0 indexed

dammit, sry, so worried in explaining how it worked i forgot to add the problem, basicaly it was the index was out of range, thanks and sorry

where/how do you initialize PossibleSkyColor? You create it wrong or use it wrong. Color[] PossibleSkyColor = new Color[2]; will allow you to use PossibleSkyColor[0] and PossibleSkyColor[1] but not PossibleSkyColor[2]!

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

planetary gravity: falling "off" 3 Answers

Cube2Sphere 2 Answers

Space and Planets 1 Answer

Perlin Noise Spherical Terrain 2 Answers

Can someone add a 2D texture alpha mask to this planet shader 0 Answers