Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by ImmanuEL · Dec 01, 2013 at 08:53 PM ·

Help! My script wont work!

Hi, I'm new to unity, I am building my first game in the engine. I ran in to problem, that is when ever you go under water it looks like you are on land so I wrote this script to add some fog every time you submerge.

 #pragma strict
 
 var WaterLevel:float;
 private var IsUnderWater:boolean;
 private var NormalColor:Color;
 private var UnderWaterColor:Color;
 
 function Start () 
 {
    NormalColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
    UnderWaterColor = new Color(0.22f, 0.65f, 0.77f, 0.5f);
 }
 
 function Update () 
 {
   if((transform.position.y < WaterLevel) != IsUnderWater)
   {
     IsUnderWater = transform.position.y < WaterLevel;
     if(IsUnderWater) SetUnderwater ();
     if(!IsUnderWater) SetNormal ();
     
   }
   
 }
 
 function SetNormal()
 {
   RenderSettings.fogColor = NormalColor;
   RenderSettings.fogDensity = 0.002f;
 }
 function SetUnderwater()
 {
   RenderSettings.fogColor = UnderWaterColor ;
   RenderSettings.fogDensity = 0.03f;
 }



I really hope one of you guys could help me, because I really need it :D

Thank you, Immanu'EL

Comment
Add comment · Show 2
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 peterpunk · Dec 01, 2013 at 09:27 PM 0
Share

You really should use problem-specific titles.

avatar image bompi88 · Dec 01, 2013 at 09:37 PM 0
Share

It's nothing wrong with the scripts logic. IsUnderWater is true whenever you are under water and false if you aren't. Have you set RenderSettings.fog = true; You should initialize the fog in the start function.

1 Reply

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

Answer by MarkD · Dec 01, 2013 at 09:21 PM

Did you check the unity wiki? http://wiki.unity3d.com/index.php/Underwater_Script

This script is the same as your script but without the mistakes.

also why did you create seperate functions for SetNormal and SetUnderWater? you can just run those in the update.

I'l write an example of your code fixed. But be sure to check the one on the wiki as you might learn from it to.

 #pragma strict
  
 var WaterLevel:float;
 //set the underwater boolean to false by standard
 private var IsUnderWater:boolean=false;
 private var NormalColor:Color;
 private var UnderWaterColor:Color;
  
 
 //you don't need this one as the Color lets you set a color in the editor
 /*
 function Start () 
 {
    NormalColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
    UnderWaterColor = new Color(0.22f, 0.65f, 0.77f, 0.5f);
 }
  */
 
 function Update () {
 
 //nothing wrong with this 
   if((transform.position.y < WaterLevel) != IsUnderWater)
   {
 
 //You tried to put a float value on a boolean, booleans can only be true or false
 // IsUnderWater = transform.position.y < WaterLevel;
 
 //you need to create a checkup to see if it is below the water level and then turn IsUnderWater to true
 //so don't try to let the bool value to check (this is wrong IsUnderWater = transform.position.y < WaterLevel;), but instead put the last part in an if statement so that the position checks
 //up on the water level.
 if(transform.position.y < WaterLevel){
 IsUnderWater=true;
 }
 //then if the position is above the water level turn it back to false. this can be done with an else statment as it needs to be opposite of the previous if statment.
 else{
 IsUnderWater=false;
 }
 
 //now that we have the script check if the player is under water, turn the boolean according to true or false, we can make a checkup on what it should do.(this happens above).
 
 
 // SetUnderwater ();
 // SetNormal ();you don't need to create a function for just two lines, place it in the if statement itself
 
 
 //we create simple if statement to react on that status of the boolean IsUnderWater and set it to true
 
 
     if(IsUnderWater){
   RenderSettings.fogColor = UnderWaterColor ;
   RenderSettings.fogDensity = 0.03f;
  }
 
 
 //and an if statement if the boolean is false
 
     if(!IsUnderWater){
   RenderSettings.fogColor = NormalColor;
   RenderSettings.fogDensity = 0.002f;
 }
 
  
   }
  
 
 }
Comment
Add comment · Show 2 · 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 ImmanuEL · Dec 01, 2013 at 10:17 PM 0
Share

Hi, Thank you so much for the help I will look at the links and the code that you posted. Thank you, Immanu'EL

avatar image MarkD · Dec 02, 2013 at 06:36 PM 0
Share

Glad I could help, don't forget to close the topic by checking the answer.

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Coyote model missing in Locomotion System 2 Answers

Help me please im stuck here Assets/MeleeSystem.js(12,38): UCE0001: ';' expected. Insert a semicolon at the end. 1 Answer

About Chinese Calendar 2 Answers

When I set an explosion prefab on my object the explosion is way off centered? 1 Answer

Mouseover script (C#) 1 Answer


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