- Home /
Water Effects
I have unity pro and im using the Water4Advanced Example they give you, how can i make it where when my player goes under water the screen has a little blue effect and where gravity lowers. Ive heard of people using some function like OnCollidisionEnter or something like that but i have no idea how i would do this myself. All help is appreciated...
Answer by aldonaletto · Oct 28, 2011 at 02:00 AM
I'm using a trigger box under the water plane, and comparing the camera position to the water plane level; when the player enters the trigger, a variable inWater is set and the water plane level is defined. The player speed is reduced when inWater is true, and if the camera position is lower than the water plane level, the underwater effects are activated - a blue fog, with the skybox replaced by a solid color (the same fog color); when the camera returns to above the water, the original settings are restored, and when the player exits the trigger, inWater is reset - something like the script below (let's call it Underwater.js), to be attached to the player:
public var uFogColor = Color(0.05, 0.36, 0.58, 1); // underwater fog public var uFogDens: float = 0.05; // underwater fog intensity // save current settings private var dFogColor: Color; private var dFogDens: float; private var dFogEn: boolean; static var inWater = false; // read this in the movement script to control the player speed public var waterLevel: float; private var underWater = false; private var lastUWater = false;
function Update(){
var cam = Camera.main; underWater = inWater && cam.transform.position.y < waterLevel; if (underWater != lastUWater){ // only change settings when underWater changed lastUWater = underWater;
if (underWater){ dFogColor = RenderSettings.fogColor; // save the current settings dFogDens = RenderSettings.fogDensity; dFogEn = RenderSettings.fog; RenderSettings.fog = true; // turn fog on... RenderSettings.fogDensity = uFogDens; // with the underwater settings RenderSettings.fogColor = uFogColor; cam.clearFlags = CameraClearFlags.SolidColor; // sky vanishes in the fog cam.backgroundColor = uFogColor; } else { RenderSettings.fog = dFogEn; // restore original settings RenderSettings.fogDensity = dFogDens; RenderSettings.fogColor = dFogColor; cam.clearFlags = CameraClearFlags.Skybox; } } }
function OnTriggerEnter(col: Collider){ if (col.tag == "Water"){ waterLevel = col.bounds.max.y; inWater = true; } }
function OnTriggerExit(col: Collider){ if (col.tag == "Water"){ inWater = false; } } In the player movement script, read inWater to know which speed to use:
function Update(){ var speed = normalSpeed; if (Underwater.inWater) speed = waterSpeed; // move the character with the speed selected ... }Since you have Pro, you can use other effects as well - like Blur, for instance.
i use c# for the playercontroller and i converted all of your code to c# other than i have to questions...
public var uFogColor is that suppose to be a string and var cam = Camera.main; is that suppose to be a transform or GameObject?
nvm i got most of it set up and just noticed that uFogColor is a float and fixed var cam but i get this error Assets/ShiftedIrony Assets/Scripts/Player/PlayerController.cs(28,41): error CS1519: Unexpected symbol `0.36' in class, struct, or interface member declaration and its with this line public float uFogColor = 0.05f, 0.36f, 0.58f, 1; // underwater fog ive had that var set up like this too and it still gives that error public float uFogColor = (0.05f, 0.36f, 0.58f, 1); // underwater fog
thank you so much this worked perfectly for me ! i have a question though, i have a simple plane for the moment and i don't know how to proceed to make it like actual water/ocean, any clue ? thanx
Answer by gfr · Oct 28, 2011 at 01:55 AM
You can use colliders set to be triggers and handle OnTriggerEnter()
/ OnTriggerExit()
.
In your handlers you would:
enable/disable screen effects (blur, color, ...)
set a flag indicating wether your character is under water
Then you can adjust the falling speed in your character controller according to that flag (assuming no rigid body).
Your answer
![](https://koobas.hobune.stream/wayback/20220613023513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Elementes like in "from dust" 1 Answer
Water effects crash mobile platform 1 Answer
How to animate Pro Water3 0 Answers
Water4 looks like fog; no surface 1 Answer
Gray water problem 1 Answer