• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Mackswel · Apr 22, 2013 at 01:11 AM · waterunderwater

How do I make the area under my water (a plane) look like its underwater?

In my game I have an area of water (The water is a plane), It looks realistic, but when I go under that plane and look up, you can't see it. How do I make it so that not only can it be seen from the below, but also so that everything under that plane has a bluish tint? I am a beginner so sorry if this sounds basic or if it's not even possible. (I've only had Unity for 6 days)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by aldonaletto · Apr 22, 2013 at 03:50 AM

The simplest solution in your case would be to double face the water plane, so that it would be rendered even from behind: attach the script from this answer to the water plane, and it will become double faced at Start.

The second part is the underwater effect. A very reasonable (and widely used) underwater effect is to turn a bluish fog on when the camera goes below the water plane. This can be done with a script like the one below attached to the player (for First Person games):

 var underwaterDens: float = 0.15;
 var underwaterColor: Color = Color(0.1, 0.3, 0.4, 1.0);
 
 private var oldFog: boolean;
 private var oldDens: float;
 private var oldColor: Color;
 private var oldMode: FogMode;
 private var curWater: Transform;
 private var underwater = false;
 
 function OnTriggerEnter(other: Collider){
     if (other.tag=="Water"){ // if entering a waterplane
         if (transform.position.y > other.transform.position.y){
             // set reference to the current waterplane
             curWater = other.transform; 
         }
     }
 }
 
 function OnTriggerExit(other: Collider){
     if (other.transform==curWater){ // if exiting the waterplane...
         if (transform.position.y > curWater.position.y){
             //  null the current waterplane reference
             curWater = null; 
         }
     }
 }
 
 function Update(){
     // if it's underwater...
     if (curWater && Camera.main.transform.position.y < curWater.position.y){
         if (!underwater){ // turn on underwater effect only once
             oldFog = RenderSettings.fog;
             oldMode = RenderSettings.fogMode;
             oldDens = RenderSettings.fogDensity;
             oldColor = RenderSettings.fogColor;
             RenderSettings.fog = true;
             RenderSettings.fogMode = FogMode.Exponential;
             RenderSettings.fogDensity = underwaterDens;
             RenderSettings.fogColor = underwaterColor;
             underwater = true;
         }
     } 
     else // but if it's not underwater...
     if (underwater){ // turn off underwater effect, if any
         RenderSettings.fog = oldFog;
         RenderSettings.fogMode = oldMode;
         RenderSettings.fogDensity = oldDens;
         RenderSettings.fogColor = oldColor;
         underwater = false;
     }
 }

Remember to tag the water plane as "Water", or nothing will happen.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tommy1235 · Apr 22, 2013 at 07:07 AM 1
Share

and you could place a particle emitter in front of the camera to create a few bubbles to make it look a little better

avatar image Mackswel · Apr 23, 2013 at 11:34 PM 0
Share

Thank You! I'll give it a try.

avatar image Mackswel · Apr 26, 2013 at 01:03 AM 0
Share

I tried it but it didn't work. Want do think I did wrong? I took the script and attached it to the camera. When I walked through the water nothing happened.

avatar image AlucardJay · Apr 26, 2013 at 01:30 AM 0
Share

Read the answer very carefully :

This can be done with a script like the one below attached to the player

avatar image Krazy0range · Dec 31, 2020 at 05:14 PM 0
Share

Can you code in Unity with Javascipt? Everybody's code samples are all Javascript. (Acually i don't really know Javascript so i don't know if it's java.)

Show more comments
avatar image
5

Answer by Kos-Dvornik · Jun 13, 2013 at 11:39 AM

Underwater tutorial: http://kostiantyn-dvornik.blogspot.com/2013/05/unity-worlds-coolest-tutorial-about.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by D3m0n · Jan 02, 2015 at 06:28 PM

I had to make some changes to make it work, I do not know why but the standard code posted which used a variable of type transform you blocking at the point "curWater = other.transform;". Instead of following the correct code to those who need it. Excellent solution in any case! :)

  var underwaterDens: float = 0.15;
  var underwaterColor: Color = Color(0.1, 0.3, 0.4, 1.0);
  
  private var oldFog: boolean;
  private var oldDens: float;
  private var oldColor: Color;
  private var oldMode: FogMode;
  private var curWater: GameObject; //CHANGED (Previous: Transform)
  private var underwater = false;
  
  function OnTriggerEnter(other: Collider){
      if (other.tag=="Water"){ // if entering a waterplane
          if (transform.position.y > other.transform.position.y){
              // set reference to the current waterplane
              curWater = other.gameObject; //CHANGED
          }
      }
  }
  
  function OnTriggerExit(other: Collider){
      if (other.gameObject==curWater){ // if exiting the waterplane...

          // CHANGED
          if (transform.position.y + 0.6 > curWater.transform.position.y) {
              //  null the current waterplane reference
              curWater = null; 
          }

      }
  }
  
  function Update(){
      // if it's underwater...

      //CHANGED
      if (curWater && Camera.main.transform.position.y < curWater.transform.position.y + 0.2){
          if (!underwater){ // turn on underwater effect only once
              oldFog = RenderSettings.fog;
              oldMode = RenderSettings.fogMode;
              oldDens = RenderSettings.fogDensity;
              oldColor = RenderSettings.fogColor;
              RenderSettings.fog = true;
              RenderSettings.fogMode = FogMode.Exponential;
              RenderSettings.fogDensity = underwaterDens;
              RenderSettings.fogColor = underwaterColor;
              underwater = true;
          }
      } 
      else // but if it's not underwater...
      if (underwater){ // turn off underwater effect, if any
          RenderSettings.fog = oldFog;
          RenderSettings.fogMode = oldMode;
          RenderSettings.fogDensity = oldDens;
          RenderSettings.fogColor = oldColor;
          underwater = false;
      }
  }

While for those who want the c # version:

 public float underwaterDens = 0.15F;
 public Color underwaterColor = new Color(0.1F, 0.3F, 0.4F, 1.0F);
 
 private bool oldFog;
 private float oldDens;
 private Color oldColor;
 private FogMode oldMode;
 private GameObject curWater;
 private bool underwater = false;
 
 void OnTriggerEnter(Collider other){
     if (other.tag=="Water"){ // if entering a waterplane
         if (transform.position.y < other.transform.position.y){
             // set reference to the current waterplane
             curWater = other.gameObject;
         }
     }
 }
 
 void OnTriggerExit(Collider other){
     if (other.gameObject==curWater){ // if exiting the waterplane...
         if (transform.position.y + 0.6F > curWater.transform.position.y){
                 //  null the current waterplane reference
                 curWater = null; 
             }
     }
 }
 
 void Update(){
     // if it's underwater...
     if (curWater && Camera.main.transform.position.y < curWater.transform.position.y + 0.2F){
         if (!underwater){ // turn on underwater effect only once
             oldFog = RenderSettings.fog;
             oldMode = RenderSettings.fogMode;
             oldDens = RenderSettings.fogDensity;
             oldColor = RenderSettings.fogColor;
             RenderSettings.fog = true;
             RenderSettings.fogMode = FogMode.Exponential;
             RenderSettings.fogDensity = underwaterDens;
             RenderSettings.fogColor = underwaterColor;
             underwater = true;
         }
 
     } else // but if it's not underwater...
         if (underwater){ // turn off underwater effect, if any
             RenderSettings.fog = oldFog;
             RenderSettings.fogMode = oldMode;
             RenderSettings.fogDensity = oldDens;
             RenderSettings.fogColor = oldColor;
             underwater = false;
         }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adam-Buckner ♦♦ · Jan 03, 2015 at 12:09 PM 0
Share

I'm not sure why you needed to make any changes... I've not tried or tested this, but, when coding...

 var myTransform : Transform;
 myTransform = other.transform;
 myTransform.position = Vector3.zero;

... should be the same as:

 var myGameObject : GameObject;
 myGameObject = other.gameObject;
 myGameObject.transform = Vector3.zero;


... but practically, it's easier to use the first example where everything is a Transform, rather than a GameObject, as it doesn't require then looking up the GameObject's Transform with each operation.

avatar image D3m0n · Jan 03, 2015 at 01:55 PM 0
Share

Yes, sure! But on my version (4.6.1f1 free) transform solution not work. I also extended slightly limits (0.2, 0.6) in order to avoid some problems in graphics output phase from the surface.

avatar image D3m0n · Jan 03, 2015 at 04:52 PM 0
Share

Now I only noticed the downvote ... sure before doing so you could at least try. Certainly I do not find useful to publish stuff changed if it does not solve a real problem. :/

I also converted the code in c#, that as it was "easy" to do it, some people can definitely make use of it without having to do it by hand.

avatar image Adam-Buckner ♦♦ · Jan 03, 2015 at 07:08 PM 0
Share

The main reason for the down-vote is the inefficiency of looking up a GameObject's Transform with every action, which is inefficient and definitely not recommended.

I appreciate the effort put into the suggested answer, but didn't want to promote a non-recommended solution.

The best solution would be getting the cached transform to work.

When I'm back from holiday, I'll see if I can find the error with the accepted solution.

avatar image
0

Answer by Adam-Buckner · May 02, 2013 at 09:55 PM

Look here: http://forum.unity3d.com/threads/20222-Water-plane-and-flip-texture

This goes into the details and has a sample script and video.

tldr; Make two water planes and flip one upside down...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

underwater shader 1 Answer

Underwater effect when entering down a slope 1 Answer

Make a water effect 1 Answer

realistic water in unity ed 0 Answers

Body of water | underwater scene 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges