Fog fade out / Decrease fog density script

So I didn’t see anyone else using a script like this so I made my own.
This script changes Linear fog end distance, but can be edited to change different values.

namespace fogStop
{
    public class fogStop : UdonSharpBehaviour
    {
        public float smooth = 2;
        private float newDistance = 20f;
        public bool trig;
        public void Update()
        {
            if (trig == true)
            {
                RenderSettings.fogEndDistance = Mathf.Lerp(RenderSettings.fogEndDistance, newDistance, Time.deltaTime * smooth);
            }
        }
        public override void OnPlayerTriggerEnter(VRCPlayerApi player)
        {
            if (player.IsValid())
            {
                if (player.isLocal)
                {
                    trig = true;
                }
            }
        }
    }
}
1 Like

Thank you, I needed to research this for a project I’m doing right now, as it happens! You just saved me some time for sure :slight_smile:

1 Like

You don’t need to check if the player is valid on that event, and you can use OnPlayerTriggerStay instead which is called on frames while a player’s capsule is inside a Trigger Collider, so you’ll get rid of that expensive update loop completely. The code will look like this:

namespace FogStop
{
    public class FogStop : UdonSharpBehaviour
    {
        public float _smooth = 2;
        private const float NEW_DISTANCE= 20f;

        public override void OnPlayerTriggerStay(VRCPlayerApi player)
        {
             if (player.isLocal)
             {
                   RenderSettings.fogEndDistance = Mathf.Lerp(RenderSettings.fogEndDistance, NEW_DISTANCE, Time.deltaTime * _smooth);
             }
        }
    }
}
1 Like