How can I make it where when my player goes under water the screen has a little blue effect and where gravity lowers
Asked Answered
O

2

0

I have Unity Pro and I’m 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?

I’ve heard of people using some methods like OnCollidisionEnter or something like that but i have no idea how I would do this myself.

All help is appreciated.

Ontogeny answered 31/3 at 21:1 Comment(0)
D
0

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.

Deciliter answered 31/3 at 21:3 Comment(2)

So you don't need Pro to do what your doing? Awesome!

Arbogast

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

Legitimatize
A
0

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).

Aeriform answered 28/10, 2011 at 1:55 Comment(0)
D
0

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.

Deciliter answered 31/3 at 21:3 Comment(2)

So you don't need Pro to do what your doing? Awesome!

Arbogast

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

Legitimatize

© 2022 - 2024 — McMap. All rights reserved.