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:
Note: code below is no C# but now-obsolete JS-like Unity Script.
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.
So you don't need Pro to do what your doing? Awesome!
– Arbogastthank 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
– Legitimatize