- Home /
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
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.
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;
}
}
}
Hi, Thank you so much for the help I will look at the links and the code that you posted. Thank you, Immanu'EL
Glad I could help, don't forget to close the topic by checking the answer.
Your answer
Follow this Question
Related Questions
Coyote model missing in Locomotion System 2 Answers
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