Creating basic underwater effects in Unity

Mukul Khanna
Dec 24, 2017 · 4 min read

CONSTRUCTING THE POOL

Making a pool with cuboids-1
Making a pool with cuboids-2

ADDING WATER PREFAB

WaterProDaytime prefab for pool water

FOGGY EFFECT

 using UnityEngine;
using System.Collections;

public class Underwater: MonoBehaviour {
public float waterHeight;

private bool isUnderwater;
private Color normalColor;
private Color underwaterColor;

// Use this for initialization
void Start () {
normalColor = new Color (0.5f, 0.5f, 0.5f, 0.5f);
underwaterColor = new Color (0.22f, 0.65f, 0.77f, 0.5f);
}

// Update is called once per frame
void Update () {
if ((transform.position.y < waterHeight) != isUnderwater) {
isUnderwater = transform.position.y < waterHeight;
if (isUnderwater) SetUnderwater ();
if (!isUnderwater) SetNormal ();
}
}

void SetNormal () {
RenderSettings.fogColor = normalColor;
RenderSettings.fogDensity = 0.01f;

}

void SetUnderwater () {
RenderSettings.fogColor = underwaterColor;
RenderSettings.fogDensity = 0.1f;

}
}

GENERATING CAUSTICS

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaterEffect: MonoBehaviour {

public float fps=30.0f; //footage fps
public Texture2D[] frames; //caustics images

private int frameIndex;
private Projector projector; //Projector GameObject

void Start(){
projector = GetComponent<Projector> ();
NextFrame();
InvokeRepeating (“NextFrame”, 1 / fps, 1 / fps);
}

void NextFrame(){
projector.material.SetTexture (“_ShadowTex”, frames [frameIndex]);
frameIndex = (frameIndex + 1) % frames.Length;
}

}
Loading images into the array
Final underwater appearance

Mukul Khanna

Written by

But in AI, as in life, there are winners and loss functions.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade