How to Increase Value as Player Approaches
I am creating a space game in which the player will traverse and explore a bunch of planets my team has built. For the atmosphere, I'd like to increase a value while the player approaches the planet from space. This value, when it's higher, will make the planet look less foggy when they arrive on the surface. When it is lower, the fog will increase. I'd like to somehow track the player's position and increase/decrease the value of the atmosphere depending on if the player is approaching or leaving the planet.
When the player approaches a world, they enter a sphere trigger collider that will initiate a bunch of functions to make gameplay on the world smooth (such as making the player and their ship children of the planet so they follow it's transform). I'd like to call this function while they are in this same trigger. So it'd be in OnTriggerStay. Since they'd become a child of the planet, that means their transform would decrease the closer they get to the planet (it'd get closer to 0, 0, 0), and I want this to increase the value on the atmosphere script. So the closer the player's transform is to the planet, increase the fog value.
Stuff like this has always stumped me. Any suggestions? I apologize if I'm not being too clear.
Hi @DaAwsumPiderman , I understood a little. From that, you need some thing to happen (less fog value) when the player reaches near the planet.
void Update () { float distance = Vector3.Distance (object1.transform.position, object2.transform.position); }
From this you will get the distance between the player and planet. Using that, you could do the other options.
For performance sake DaAwsumPiderman should use Vector3.sqr$$anonymous$$agnitude() since the value will probably be updated a lot (each frame). Although this might cause unwanted effects if the fog-effect is a direct cause of the distance (since sqr$$anonymous$$agnitude is the distance squared the fog-effect will have exponential growth/decay if you use something like float fog = 1/Vector3.sqr$$anonymous$$agnitude();)
This allows me to find the distance, but it slows my game down significantly. What I'm looking for is a way to decrease a value as another value increases. I need to decrease the fog float (which will increase my custom fog...) as the player's distance from the planet increases. Thanks for your reply though! I appreciate the help. Do you know how I can accomplish the decrease & increase thing?
@DaAwsumPiderman Did you even read my comment? Use Vector3.Sqr$$anonymous$$agnitude! It's faster than Vector3.Distance.
Also I answered you question. 1/Vector3.sqr$$anonymous$$agnitude is a value that will DECREASE when sqr$$anonymous$$agnitude INCREASES.
Your answer
Follow this Question
Related Questions
Can I set a halo of light so that it stays lit when the camera is near it 0 Answers
Move Character in the way he is facing.,Make character move in way he is facing. 1 Answer
how to load browsed video name in canvas? 1 Answer
Player teleport not working 0 Answers
Diagonal character movement 0 Answers