Search Unity

Underwater script is not working.

Discussion in 'Scripting' started by bangoo, Apr 9, 2021.

  1. bangoo

    bangoo

    Joined:
    Apr 25, 2015
    Posts:
    12
    Why is the following script not working (unity 2021) in my scene?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Underwater: MonoBehaviour {
    4. public float waterHeight;
    5. private bool isUnderwater;
    6. private Color normalColor;
    7. private Color underwaterColor;
    8. // Use this for initialization
    9. void Start () {
    10. normalColor = new Color (0.5f, 0.5f, 0.5f, 0.5f);
    11. underwaterColor = new Color (0.22f, 0.65f, 0.77f, 0.5f);
    12. }
    13. // Update is called once per frame
    14. void Update () {
    15. if ((transform.position.y < waterHeight) != isUnderwater) {
    16. isUnderwater = transform.position.y < waterHeight;
    17. if (isUnderwater) SetUnderwater ();
    18. if (!isUnderwater) SetNormal ();
    19. }
    20. }
    21. void SetNormal () {
    22. RenderSettings.fogColor = normalColor;
    23. RenderSettings.fogDensity = 0.01f;
    24. }
    25. void SetUnderwater () {
    26. RenderSettings.fogColor = underwaterColor;
    27. RenderSettings.fogDensity = 0.1f;
    28. }
    29. }
    The script is taken from here.
    I attach the script to the camera and if the camera is below the position y set in the variable - nothing happens.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Try Debug.Log-ing transform.position.y alongside waterHeight and see if those values are what you think they are.
     
  3. bangoo

    bangoo

    Joined:
    Apr 25, 2015
    Posts:
    12
    I have displayed the variables in UI, the boolean is changing to true. somthing is wrong with the colors settings.
     
  4. bangoo

    bangoo

    Joined:
    Apr 25, 2015
    Posts:
    12
    Maybe I have found the problem - fog doesn't work with my amd radeon graphics card.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    Try the linear fog... I believe it works on a wider range of targets. It's what I use for mobile too.