I have written code and have done everything correctly at every turn yet my underwater fog fights me every step of the way, I can usually think of something that fixes enough for me to work with the problem more until I can figure it out but I am fresh out of ideas other than restarting completely. I seriously need help with this issue I have no idea why something that should be so simple is fighting me so badly.
I have created a water plane made it look good with my custom water material (default shaders), then made a box sized and positioned it perfectly where i need it, made the box a child of the water plane and attached a script to the box, then made the whole thing a prefab and dragged them onto the scene where i need the,
here is what someone has written to try and help me so far.
[LIST=1]
[*]using UnityEngine;
[*]using System.Collections;
[*]
[*]public class test : MonoBehaviour {
[*] public Camera camera;
[*] public Renderer renderer;
[*]
[*] private bool defaultFog = false;
[*] private Color defaultFogColor = Color.black;
[*] private float defaultFogDensity = 0;
[*]
[*] public Color topFogColor = Color.red;
[*] public Color bottomFogColor = Color.blue;
[*] public float fogDensity = 0.075f;
[*] public float maxColorDepth = 10;
[*]
[*] // Use this for initialization
[*] void Start () {
[*] camera = Camera.main;
[*] if (camera == null) camera = Camera.allCameras[0];
[*] defaultFog = RenderSettings.fog;
[*] defaultFogColor = RenderSettings.fogColor;
[*] defaultFogDensity = RenderSettings.fogDensity;
[*] renderer = gameObject.GetComponent<Renderer>();
[*] }
[*]
[*] // Update is called once per frame
[*] void Update () {
[*] if (camera == null || renderer == null) return;
[*]
[*] // test fog according to the bounding box of the water "box")
[*] SetFog(renderer.bounds.Contains(camera.transform.position));
[*] }
[*]
[*] void SetFog(bool underWater)
[*] {
[*] if (!underWater) {
[*] RenderSettings.fog = defaultFog;
[*] RenderSettings.fogColor = defaultFogColor;
[*] RenderSettings.fogDensity = defaultFogDensity;
[*] return;
[*] }
[*]
[*] var depthAmount = (renderer.bounds.max.y - camera.transform.position.y) / maxColorDepth;
[*]
[*] var fogColor = Color.Lerp(topFogColor, bottomFogColor, depthAmount);
[*]
[*] RenderSettings.fog = true;
[*] RenderSettings.fogColor = fogColor;
[*] RenderSettings.fogDensity = fogDensity;
[*] }
[*]}
[/LIST]
the problem with this code is it only works with one prefab, like the script itself can only work once in the whole scene, I have no idea what is wrong with it and he is better at coding than me so to me it looks pretty flawless but I am not 100% certain since some of it is kind of beyond my skills right now.
*Thanks to bigmisterb for writing this