Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        targetRainIntensity = 0.0f;
        targetSnowIntensity = 0.0f;

        switch (currentWeather)
        {
        case WeatherStates.Rain:
            targetRainIntensity = intensity;
            break;

        case WeatherStates.Snow:
            targetSnowIntensity = intensity;
            break;

        default:
            break;
        }

        fog.m_Intensity = fogIntensity;

        // update skybox transition
        if (fogIntensity > 0 || intensity > 0 && (currentWeather == WeatherStates.Rain || currentWeather == WeatherStates.Snow))
        {
            skyboxLerp = Mathf.Min(1, skyboxLerp + Time.deltaTime * WEATHER_CHANGE_SPEED);
        }
        else
        {
            skyboxLerp = Mathf.Max(0, skyboxLerp - Time.deltaTime * WEATHER_CHANGE_SPEED);
        }
        RenderSettings.skybox.SetFloat("_Blend", skyboxLerp);

        float delta = Time.deltaTime * WEATHER_CHANGE_SPEED;

        rain.m_RainIntensity += Mathf.Sign(targetRainIntensity - rain.m_RainIntensity) * Mathf.Min(delta, Mathf.Abs(targetRainIntensity - rain.m_RainIntensity));
        snow.m_Intensity     += Mathf.Sign(targetSnowIntensity - snow.m_Intensity) * Mathf.Min(delta, Mathf.Abs(targetSnowIntensity - snow.m_Intensity));

        // make weather follow player
        try
        {
            snow.setEmitterPosition(Camera.main.transform.position + new Vector3(0, 4, 0));
            fog.setEmitterPosition(Camera.main.transform.position + new Vector3(0, 4, 0));
            rain.setEmitterPosition(Camera.main.transform.position + new Vector3(0, 20, 0));
        }
        catch (System.Exception e)
        {
        }

        if (prevPos == null)
        {
            try
            {
                prevPos = Camera.main.transform.position;
            }
            catch (System.Exception e)
            {
            }
            return;
        }
        else
        {
            try
            {
                float distanceDelta = Vector3.Distance(Camera.main.transform.position, prevPos);

                float speed = distanceDelta / Time.deltaTime;
                speedSamples.Enqueue(speed);
                runningSum += speed;

                if (speedSamples.Count > MAX_SAMPLES)
                {
                    float v = speedSamples.Dequeue();
                    runningSum -= v;
                }

                float runningAvg = runningSum / speedSamples.Count;

                if (currentWeather == WeatherStates.Rain && runningAvg > rainThreshold ||
                    currentWeather == WeatherStates.Snow && runningAvg > snowThreshold /* ||
                                                                                        * currentWeather == WeatherStates.Ice && speed > iceThreshold*/)
                {
                    print("Slip! Move slower!");
                }

                prevPos = Camera.main.transform.position;
            }
            catch (System.Exception e)
            {
            }
        }

        windZone.windMain = Mathf.Lerp(0, 60, windStrength);
        Vector3 rot = windZone.transform.rotation.eulerAngles;

        rot.y = windDirection;
        windZone.transform.rotation = Quaternion.Euler(rot);
    }